16.01.2013 Views

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The SPProperty Bag Class<br />

Chapter 33: The Windows SharePoint Services Object Model 935<br />

The SPPropertyBag class of the <strong>Microsoft</strong>.SharePoint.Utilities namespace stores arbitrary<br />

key-<strong>and</strong>-value pairs that contain custom property settings. First you have to make<br />

an instance of the SPGlobalAdmin class in the <strong>Microsoft</strong>.SharePoint.Administration<br />

namespace. Next the Config property of the SPGlobalAdmin class gets a global configuration<br />

object, SPGlobalConfig. The Properties property of the SPGlobalConfig class<br />

will contain global configuration settings that are used in the Windows SharePoint Services<br />

deployment. In the following code example, we will add a key-<strong>and</strong>-value pair to<br />

our property bag by using the Add method. Modifications to the property bag will not<br />

be saved until we call the Update method of the SPPropertyBag class.<br />

string strReturn = "";<br />

SPGlobalAdmin myGlobalAdmin = new SPGlobalAdmin();<br />

myGlobalAdmin.AllowUnsafeUpdates = true;<br />

SPGlobalConfig myGlobalConfig = myGlobalAdmin.Config;<br />

SPPropertyBag myBag = myGlobalConfig.Properties;<br />

int myValue;<br />

try<br />

{<br />

myValue = Int32.Parse(myBag["mykey"]);<br />

myValue++;<br />

myBag["mykey"] = myValue.ToString();<br />

}<br />

catch (Exception err)<br />

{<br />

myValue = 1;<br />

myBag.Add("mykey", myValue.ToString());<br />

strReturn += err.Message;<br />

}<br />

myBag.Update();<br />

strReturn = myValue.ToString();<br />

output.Write(strReturn);<br />

The SPEncode Class<br />

The SPEncode class from the <strong>Microsoft</strong>.SharePoint.Utilities namespace provide static<br />

methods so you do not need to instantiate the class before using them. You may use<br />

the methods of the SPEncode class to help prevent malicious script blocks from<br />

being able to execute in applications that execute across sites. In code that calls<br />

members within the Windows SharePoint Services assembly, the encoding methods<br />

of the SPEncode class are more effective than methods of the System.Web.HttpUtility<br />

<strong>and</strong> System.Web.HttpServerUtility classes. The SPEncode methods for encoding URLs<br />

<strong>and</strong> HTML can be used to encode a larger set of characters than the methods of the<br />

HttpUtility <strong>and</strong> HttpServerUtility classes.<br />

The following code example makes use of the ScriptEncode method to escape<br />

characters that would otherwise conflict with the script. For example, a quotation<br />

mark (“) is replaced with \”.<br />

strReturn = SPEncode.ScriptEncode("alert(\"hi\")");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!