Tuesday, July 31, 2012

How to know if the Page is in Edit Mode - C#

For a page we need to use:
if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   //  display mode
  }
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   //  edit mode
  }



and for webpart pages:

   WebPartManager mgr = this.WebPartManager;
 if (mgr.DisplayMode == WebPartManager.EditDisplayMode)
     {
        // edit mode
     }
  else
     {
  //  display mode
     }

How to know if the Page is in Edit Mode - JavaScript ?

For a page we need to use:

var pageMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;

if (pageMode == 1)
{
// page is in edit mode
}
else
{
// page is in browse mode
}



and for wiki pages:



var pageMode = document.forms[MSOWebPartPageFormName]._wikiPageMode.value;

if (pageMode == "Edit")
{
// page is in edit mode
}
else
{
// page is in browse mode
}



This will refer to a value of the following html input control, which is rendering on the page when it is in edit mode:






Monday, July 30, 2012

Page Layout issue - Show/Hide fields in Display/Edit Mode

If you want to hide/display fields in edit/display mode on your page layout , you can do it by adding publishingwebcontrols:editmodepanel tag.




Put your fields here


or



Put your fields here

Thursday, July 12, 2012

The same client browser session has made '6' requests in the last '11' seconds.

After the configuration of ADFS v2 to SharePoint 2010 and
when I tried to login, I found at myself that after I authenticate to ADFS, get caught up in this endless loop where go back and forth between SharePoint and ADFS.
In Fiddler it turns out that I are authenticating successfully to ADFS, I am coming back to SharePoint and it is successfully issuing the FedAuth cookie,
it redirects you to /_layouts/authenticate.aspx on the SharePoint site which clears out the FedAuth cookie and redirects me back to the ADFS site.
I basically ping pong back and forth until ADFS stops it .

In Event Viewer of ADFS Server :



I saw the exception:



Exception details:
Microsoft.IdentityServer.Web.InvalidRequestException: MSIS7042: The same client browser session has made '6' requests in the last '11' seconds. Contact your administrator for details.
at Microsoft.IdentityServer.Web.FederationPassiveAuthentication.UpdateLoopDetectionCookie()
at Microsoft.IdentityServer.Web.FederationPassiveAuthentication.SendSignInResponse(MSISSignInResponse response)



That’s because the default LogonTokenCacheExpirationWindow for the SharePoint STS is 10 minutes. In this case when I created my relying party by default it sets the token lifetime in ADFS to be 2 minutes, so as soon as it authenticated it knew the cookie was good for less time than the LogonTokenCacheExpirationWindow value. Therefore it went back to ADFS to authenticate again. And so it went, back and forth. So I needed to change the LogonTokenCacheExpirationWindow to be less than the SAML TokenLifetime.

The solution is PowerShell Script:







$sts = Get-SPSecurityTokenServiceConfig
$sts.LogonTokenCacheExpirationWindow = (New-TimeSpan –minutes 1)
$sts.Update()
iisreset

Tuesday, July 10, 2012

The workbook cannot be opened



This is error I received on any excel file, which I tried to open in browser by Office Web Apps.

The solution was granting permissions to the service account for Excel Services.
The PowerShell script, which invokes the SPWebApplication.GrantAccessToProcessIdentity method helps to solve it:



Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0

$webApp = Get-SPWebApplication "http://contoso.com"

$webApp.GrantAccessToProcessIdentity("CONTOSO\spExcelServiceApp")

Monday, July 9, 2012

Create and Retrieve a Social Comment - C#

The SocialCommentManager object enables you to create a social comment for any specified URL. This topic demonstrates how to use the SocialCommentManager to create and retrieve social comments in a custom application.

Creating Social Comments using c#:


using (SPSite site = new SPSite("http://contoso"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
mySocialCommentManager.AddComment(new Uri("My URL"), “My Comment”);
}



Retrieving Social Comments using c#:


public class PageComments
{
public string Comment { get; set; }
public UserProfile CommentOwner { get; set; }
public DateTime CommentDate { get; set; }
}


public class GetCommentsClass
{

public PageComments GetCommentsByPage(SPListItem item,int prevDates)
{
PageComments pc = new PageComments();

SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
SocialComment[] comments = mySocialCommentManager.GetComments(new Uri(Site + "/" + item.File.Url));

foreach (SocialComment comment in comments)
{
if (comment.LastModifiedTime.ToLocalTime().CompareTo(DateTime.Now.AddDays(previousDates)) >= 0)
{
pc.Comment = comment.Comment;
pc.CommentDate = comment.LastModifiedTime;
pc.CommentOwner = comment.Owner;

}
}

return pc;
}
}