Posted by: R Manimaran | July 13, 2010

useful Microsoft SharePoint Utilities Namespace Methods

Microsoft Sharepoint has a namespace Microsoft.SharePoint.Utilities which has some useful class & methods. Following are the some of the useful methods with example.

Namespace: Microsoft.SharePoint.Utilities

Class: SPEncode

Method:  HtmlEncode

Description: Encodes a HTML string

Example:

string inputText = “<b>Worpress   Blog</b>”;

string EncodedText=SPEncode.HtmlEncode(inputText);

Console.WriteLine(“HTMLEncoded Text:”+EncodedText);

O/p:

&lt;b&gt;Worpress   Blog&lt;/b&gt;

—————————————————————————–

Method: HtmlDecode

Description:  Decodes a encoded HTML string to HTML

Example:

Console.WriteLine(“Decoded Text:”+SPEncode.HtmlDecode(EncodedText));

O/p: <b>Worpress   Blog</b>

—————————————————————————–

Method: HtmlEncodePreserveSpaces

Description: Encode the HTML string. It Preserves the spaces

Example:

Console.WriteLine(SPEncode.HtmlEncodePreserveSpaces(inputText));

O/p:

<b>Worpress&nbsp;&nbsp; Blog</b>

—————————————————————————–

Method:  IsLegalCharInUrl

Description: Checks the URL for Legal Characters

Example: Check the url string for any Illegal URL characters.

There are some illegal Url characters.

# % & * { } \\ : < > ? /

string FileName=”# % & * { } \\ : < > ? /SiteUrl “;

char[] NameChars=FileName.ToCharArray();

foreach(char ch in NameChars)

{

if((!SPEncode.IsLegalCharInUrl(ch)))

{

FileName=FileName.Replace(ch.ToString(),””);

}

}

//Check for spaces

FileName=FileName.Replace(” “, “”);

O/p: SiteUrl

—————————————————————————–

Method: UrlEncodeAsUrl

Description: Encodes a URL string

Example:

string url = “http://r.manimaran.wordpress.com/Corporate Site”;

Console.WriteLine(SPEncode.UrlEncodeAsUrl(url));

O/p:  http://r.manimaran.wordpress.com/Corporate%20Site

—————————————————————————–

Method: UrlDecodeAsUrl

Description: Decodes a URL string

Example:

Console.WriteLine(SPEncode.UrlDecodeAsUrl(“http://r.manimaran.wordpress.com/Corporate%20Site”));

O/p: http://r.manimaran.wordpress.com/Corporate Site

—————————————————————————–

Class : SPUtility

Method: CreateISO8601DateTimeFromSystemDateTime

Description: Creates an ISO8601 DateTime From from the sytem Date Time

ISO8601 DateTime Format: YYYY-MM-DDThh:mm:ss

Example:

Console. WriteLine (SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now));

O/p: 2010-07-09T15:23:32Z

—————————————————————————–

Method: CreateDateTimeFromISO8601DateTimeString

Description: Returns a DateTime Object for the specified ISO8601 string Format.

Example:

DateTime dob = SPUtility.CreateDateTimeFromISO8601DateTimeString(“2010-07-09T14:52:18Z”);

Console.WriteLine(dob.ToString());

O/p:

7/9/2010 2:52:18 PM

—————————————————————————–

Method: SPUtility.FormatDate

Description: Converts the specified date and time value to a SPDateFormat format.

It converts the DateTime to GMT format.

SPDateFormat enumerations

  • SPDateFormat.DateOnly
  • SPDateFormat.DateTime
  • SPDateFormat.ISO8601
  • SPDateFormat.MonthDayOnly
  • SPDateFormat.TimeOnly

Example:

Console.WriteLine(SPUtility.FormatDate(web, DateTime.Now, SPDateFormat.DateOnly));

Console.WriteLine(SPUtility.FormatDate(web, DateTime.Now, SPDateFormat.DateTime));

Console.WriteLine(SPUtility.FormatDate(web, DateTime.Now, SPDateFormat.ISO8601));

Console.WriteLine(SPUtility.FormatDate(web, DateTime.Now, SPDateFormat.MonthDayOnly));

Console.WriteLine(SPUtility.FormatDate(web, DateTime.Now, SPDateFormat.TimeOnly));

O/p:

7/9/2010

7/9/2010 9:04 PM

2010-07-09T21:04:23Z

July 09

9:04 PM

In my site collection I have set the Regional settings as GMT+5:30 i..e IST time format.

I changed the Time format to GMT-8:00 Pacific Time zone.

Console.WriteLine(SPUtility.FormatDate(web, web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.Now), SPDateFormat.DateTime));

O/p:

Output the current time in US and Canada as 7/10/2010 3:32 AM

—————————————————————————–

Method: HexStringToLong

Description: Converts a Hexa decimal to long

Example:

long lng=SPUtility.HexStringToLong(“A10”);

O/p: 2576

—————————————————————————–

Method: TimeDeltaAsString

Description: Show the time difference between two time value as String

Example:

DateTime time1=DateTime.Now.AddHours(-2);

DateTime time2=DateTime.Now.AddHours(3);

string strTime= SPUtility.TimeDeltaAsString(time1, time2);

O/p:

5 hours ago

—————————————————————————–

Class:  SPStringUtility

Method:  RemoveNonAlphaNumericChars

Description: Remove the alphanumeric characters in a string

Example:

string test =SPStringUtility.RemoveNonAlphaNumericChars(“ABCD1234$%^”);

O/p: ABCD1234

—————————————————————————–

Method: RemoveControlChars

Description: Remove the control characters from a given string

Control characters “\a, \b,\t, \n, \f, \r, ”

Example:

string controlChar=SPStringUtility.RemoveControlChars(“Test\a\b\t\n\f\r”);

O/p:

Test

—————————————————————————–

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: