SPField

Updating the title within a SPItemEventReceiver with AfterProperties

Recently I had a problem setting the title field of a Page within the pages library. My requirement was to set the title with the value from the PageTitle field of the current item. An ItemEventReceiver, which is executed synchronously to prevent save conflict exception or problems with published items, was supposed to do exactly that. But when I set the title property of the item via AfterProperties, the value did not get stored.

Custom field and UpdateFieldValueInItem()

Recently I was developing a custom field. To store modified values, the UpdateFieldValueInItem method has to be overwritten. In a normal way of clicking the submit/save button, the method is called and I can adjust the value for the field within the current item. The changes are submitted to the database later. But what if you want to modify items outside of the current item? Sure, you can do so would you think.

StaticName != InternalName

Recently I was trying to fetch a SPField from a SPWeb object. I had SharePoint 2010, so I decided to use the new SPFieldCollection.TryGetFieldByStaticName() Method. You can imagine how surprised I was, that I couldn’t get the field I was looking for. What do we learn? Well, the StaticName of an SPField is not necessarily the InternalName! Here is a link to the MSDN about SPField.StaticName: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.staticname.aspx

Issues in WSS V2

Responses to items in an Issue list (SPListIssue) are new version in WSS V3. WSS V2 lacks the ability to use versions for lists. So what did Microsoft do that an Issue list behaves like it is using versions? In WSS V2 items in a single “thread” all have different ItemIDs. To group them together, all have the same IssueID. This is the ItemID from the original item. A single issue in WSS V2 could look like this:

SPQuery, ViewFields and empty fields

If you want your query to return empty columns, you have to add Nullable=’TRUE’ to the viewfields. If you do not add the Nullable attribute, accessing the results of the query like this: oListItemAvailable[“Field1”] will give an Exception. I’ve made posted a comment about this on the page in the MSDN http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx

Supported field types as source for Lookup Fields

Lookup fields are great. They can show information from other lists. I have never noticed before, that the only two supported field types for lookups are SPFieldText and SPFieldCalculated. All other field types can not be used as source for lookup fields.Technorati Tags: SharePoint,SPFieldLookup,SPFieldText,SPFieldCalculated

SharePoint Bug with MultiValue Fields

Imagine you have a list (or document library) which has lots of items. Nothing fancy here. Now add a lookup or user field and allow it to contain multiple values. Inside a view for this list you can filter e.g. for the title column. The filter dropdown shows all possible values for the column. Here comes the clue. If the item count of the list reaches a number somewhere between 400 and 500 items, the filter dropdown changes.

Programmatically creating a SPFieldCalculated

As you might already know, you can create new fields with SPFieldCollection.AddFieldAsXml(string schema). The schema contains the formula for the calculated field. There are some points to take care of, before you can add the field: make sure your referenced fieldnames are the display names and not the internal field names the formula has to be in the English format Changing the fieldnames is an easy task. If you read the schema from a field and want to create a new one with the same formula, you will get something like “=if(fieldA,1,2)”.

Adding a custom field type via code

Your custom field type can be added to a list in a browser easily. But how do you add a custom field type via code? Here is my way: add a new field with the field type from which your custom field type derived change the field type of the new field to your own custom field type In my case my custom field type derived from a SPFieldLookup. 1: // create new lookup field <pre>&lt;font size=2>&lt;span class=lnum> 2: &lt;/span>&lt;span class=kwrd>string&lt;/span> newFieldName = fields.

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.