SPField

HowTo create an object if you only have its type as string

Sometimes you have only the type of an object, which you want to create. Meaning you want to create an object dynamically. This is how you would achieve your goal: In my case I wanted to create a SPField from its typename. 1: string typename = “Microsoft.SharePoint.SPFieldText, Microsoft.SharePoint, “+ <span class=lnum> 2: </span> <span class=str>"Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"</span>; 3: Type t = Type.GetType(typename, true, true); <span class=lnum> 4: </span><span class=kwrd>object</span> newObject = System.

How to get LookupField Information from a listItem

If you want the ID or the value form a LookupField, you can get it easily with this code snippet:SPListItem item = getitsomewhare… SPFieldLookupValue lf = (SPFieldLookupValue) item.ParentList.Fields.GetField(_FieldName).GetFieldValue( item.GetFormattedValue(_FieldName)); if you got the field, fetch its properties viaif (lf == null) { int itemID = lf.LookupId; string itemValue = lf.LookupValue; } Have fun ;-)

Write a SPFieldUser

If you have a list which contains a SPFieldUser field (with multiple selection), you can add users too it with the following code: using (SPSite site = new SPSite(“http://site”)) { using (SPWeb web = site.AllWebs[“Web”]) <span style="font-family:Consolas;font-size:10pt"> {<br /> </span> <span style="font-family:Consolas;font-size:10pt"> <span style="color:#2b91af">SPList</span> list = web.Lists[<span style="color:#a31515">"List"</span>];<br /> </span> <span style="font-family:Consolas;font-size:10pt"> <span style="color:#2b91af">SPListItem</span> item = list.Items[0];<br /> </span> <span style="font-family:Consolas;font-size:10pt"> <span style="color:#2b91af">SPFieldUserValueCollection</span> values = (<span style="color:#2b91af">SPFieldUserValueCollection</span>)item[<span style="color:#a31515">"Users"</span>];<br /> </span> <span style="font-family:Consolas;font-size:10pt"> <span style="color:#2b91af">SPUserCollection</span> users = web.