Friday, 21 January 2011

The Substitution Control


I hold the Substitution Control in highest regard, since it has saved my professional life, more than once.

The Substitution Control should be used in an output-cached Page, in a section that you want to display dynamic content.

In a nutshell, used it to punch some holes in your cached web page.


Here is an example of usage:

You have a SharePoint Web Page, that is output-cached.

However, this page has on the top, a user control that displays a welcome phrase to the current user:


Your markup should be something like this (if you are in a Masterpage):

<body .....>
......

<usercontrol1:WelcomeUser ID="WelcomeUser1" runat="server" />
....
</body>

To display the correct current context user, despite the output cache, you'll need to use the Substitution Control.

The WelcomeUser Control, should be something like this:

<asp:Substitution id="Substitution1" methodname="GetCurrentUser" runat="server">
</asp:Substitution>

The methodname property is the name of the method that will return the information you'll need to render in the Substitution section.

This method must:
  • Be a static method
  • Accept a parameter of type HttpContext
  • Return a value of type String.

For this example, you'll have something like this in the code behind:

public static string GetCurrentUser(HttpContext context)
{
try
{
SPUser user = SPControl.GetContextWeb(context).CurrentUser;
StringBuilder builder = new StringBuilder();
builder.Append("<div class='cssClass1;'>");
builder.Append("<div class='cssClass2;'>");
builder.Append(string.Format("Welcome, {0}",user.Name));
builder.Append("</div>");
builder.Append("</div>");

return builder.ToString();
}
catch (Exception innerException)
{ //LOG ERROR
return string.Empty;
}
}



Importante note: This control will not work inside an Update Painel.

Thursday, 20 January 2011

Synchronize SharePoint 2007 content with Outlook

To synchronize a SharePoint Document Library with your outlook, the secret is in the uri create by the SharePoint.

For example, if you go to a Document Library and click on the Actions Menu, you can see the Connect to Outlook option:






If you click on that option, it will generate a Uri that will be understood by the Outlook:




If, by any means, you need to create this Uri programmatically, no problem. It's just a simple Uri with some basic querystring parameters.
  • stssync://sts/?ver=1.1&type=documents&cmd=add-folder&base-url=[URI WITH URL ENCODE]&list-url=[LIST URI WITH URL ENCODE]&guid=[DOCUMENT LIBRARY GUID WITH URL ENCODE]&site-name=[SITE NAME WITH URL ENCODING]&list-name=[LIBRARY NAME WITH URL ENCODING]

Example:
  • stssync://sts/?ver=1.1&type=documents&cmd=add-folder&base-url=http%3A%2F%2Fmy.site.com%26list-url=%2FDocuments%2F&guid=%7Bb83d3dff-7d6e-41c1-83de-3e3c9a831f98%7D&site-name=My%20Site&list-name=Documents



Good Luck.
HD