Tuesday, January 25, 2011

Some useful methods in SPUtility class

SPUtility is a very rich class, which contains a number of useful functions; it also contains a number of obsolete functions. I found following methods very useful in our daily programming. 

1. System DateTime to ISO8601 DateTime
Converts a system DateTime value to ISO8601 DateTime format (yyyy-mm-ddThh:mm:ssZ)
string isoDateTime = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now());
//Output: 2010-03-30T15:30:33Z

2. Redirect
Signature of Redirect method:
public static bool Redirect(string url, SPRedirectFlags flags, HttpContext context)
The below link will provide you detailed information about the SPRedirectFlags.
3. Transfer to Success or Error Page
These methods are useful when you are
    SPUtility.TransferToErrorPage("Hi this is an Error Page", "this is link","{your_url}");

    SPUtility.TransferToErrorPage("Hi this is an Error Page {0}, {1}", "click here to Go to Top site", "{your_url}");
 
    SPUtility.TransferToSuccessPage("Hi this is an Sucess Page");
    
    SPUtility.TransferToSuccessPage("Hi this is an Sucess Page {0},{1}", "{url}", "This is Link", "{url}");
Useful link:
4. Email
Send an email from the context of the given SPWeb.
SPWeb web = SPContext.Current.Site.OpenWeb();
string subject = "Email from the " + web.Title + " web";
string body = "The body of the email";
SPUtility.SendEmail(web, false, false, "someone@somewhere.com", subject, body);

5. 12-hive Path:
Returns the filesystem path for the 12-Hive, or any of the folders beneath it.
(C:\Program Files\Common Files\Microsoft Shared\web server extensions\12)
string featuresPath = SPUtility.GetGenericSetupPath("Template\\Features");

6. Full URL:
Return absolute url from relative url
string webUrl = "/sub/default.aspx";
SPUtility.GetFullUrl(SPContext.Current.Site, webUrl);
//Output: "http://localhost/sub/default.aspx"

There are methods that I did not describe; I will publish them as soon as I find out how they can be used.

Reference Links:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility_members.aspx

Monday, January 24, 2011

SPWeb.ProcessBatchData method to improve SharePoint list operations performance

The common way to add or update multiple items in SharePoint list is SPListItem.Update. You can an existing item or add a new item using SplistItemCollection .Add(). 

Sample Code Snippet:

for (int itemCount = 0; itemCount < 100;s itemCount++)
{
SPListItem newItem = listItemCollection.Add();
newItem.Update();
}

Using Batch Update:
ProcessBatchData method takes XML format as input and it will directly communicate with SharePoint content database.

StringBuilder query = new StringBuilder();
query.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
for (int itemCount = 0; itemCount < 100; itemCount++)
{
query.AppendFormat("<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"{3}Title\">{2}</SetVar>" +
"</Method>", i, listGuid, someValue, "urn:schemas-microsoft-com:office:office#");
}
query.Append("</Batch>");
spWeb.ProcessBatchData(query.ToString());


Conclusion:
If you have to update a larger number of items its highly recommended to not use the Update method on every item. Instead – use the batch update function ProcessBatchData provided by SPWeb.


Reference Links:

Friday, January 14, 2011

SharePoint 2010: Version History for Multiline text column with “Append Changes to Existing Text” not visible in SharePoint 2010 Custom Edit forms

Problem Statement:
If you have created a Multiline textbox with “Append Changes to Existing Text”
This will display a version history for that column in default Edit and Display form, but the same thing won’t work in the custom edit and display forms.
Solution:
Add tag below the text fields control with Control mode “Display” and unique Id.
<SharePoint:AppendOnlyHistory runat="server" id="ff21disp{$Pos}" controlmode="Display" fieldname="Comments "></SharePoint:AppendOnlyHistory>

Monday, January 10, 2011

Introduction...

Hey Guys,
This is my first BLOG post. I always think to write any blog on SharePoint but frankly speaking, I didn’t have the time last year due to project deliverables. I now realize that there is so much information that I came across which is truly useful and I don’t always have time to write a blog post. This year I have decided to give my time to this blog. Thanks for reading and I hope all my time and effort helps you on many levels.