In one of my current projects, in which I am programmatically adding a new SPListItem to the Sites list (created from the Site Directory Template), I couldn't figure out how to get the Owners name to save.
/* Doesn't save the Owner information to the Sites list */
SPListItem item = siteDirectoryList.Items.Add();
item[item.Fields.GetFieldByInternalName("Owner").Id] = string.Format("{0};#{1}", user.ID.ToString(), user.Name);
After a number of fruitless effects to try and set the Owner field properties, I decided to take a look at all the available fields that are part of the Sites List. The list below is all the fields that were returned for my instance of Sites. Your may contain more or less depending on how much you customize your Sites List.
ContentTypeId Title _ModerationStatus _ModerationComments File_x0020_Type
SiteTitle SiteURL Description Owner Division
Region DivisionMulti RegionMulti BestBet URL
TopSite TasksAndTools OwnerNew URLStatus Manager
ID ContentType Modified Created Author
Editor _HasCopyDestinations _CopySource owshiddenversion WorkflowVersion
_UIVersion _UIVersionString Attachments Edit LinkTitleNoMenu
LinkTitle SelectTitle InstanceID Order GUID
WorkflowInstanceID FileRef FileDirRef Last_x0020_Modified Created_x0020_Date
FSObjType PermMask FileLeafRef UniqueId ProgId
ScopeId HTML_x0020_File_x0020_Type _EditMenuTableStart _EditMenuTableEnd
LinkFilenameNoMenu LinkFilename DocIcon ServerUrl EncodedAbsUrl
BaseName MetaInfo _Level _IsCurrentVersion TitlewURL
TitlewMenu
As you can see, there are many hidden fields that are used to keep the Sites List working. Needless to say, there are two fields relating to the Owner field; Owner & OwnerNew. So, I tried setting the OwnerNew field of my Sites list and it worked!! See sample code below…
public static void AddSiteItem(string siteDirectoryURL, string title, string url, string desc)
{
/* Open up a SPWeb to the Site Directory */
SPSite portalSite = new SPSite(siteDirectoryURL);
SPWeb siteDirectory = portalSite.OpenWeb();
/* Get the Sites List */
SPList siteDirectoryList = siteDirectory.Lists["Sites"];
portalSite.AllowUnsafeUpdates = true;
/* Create a new item in memory */
SPListItem item = siteDirectoryList.Items.Add();
/* Grab current user to be used as the owner */
SPUser user = SPContext.Current.Web.CurrentUser;
/* Set default properties */
item[item.Fields.GetFieldByInternalName("Title").Id] = title;
item[item.Fields.GetFieldByInternalName("URL").Id] = url;
item[item.Fields.GetFieldByInternalName("Description").Id] = desc;
/* Set owner field using field OwnerNew vs. Owner */
item[item.Fields.GetFieldByInternalName("OwnerNew").Id] = string.Format("{0};#{1}", user.ID.ToString(), user.Name);
/* Update / Save item */
item.Update();
portalSite.AllowUnsafeUpdates = false;
}
Who would have thought? Anyways, hopefully if anyone else has to do this, this will help.
As a side note, I am using item.Fields.GetFieldByInternalName because I have multiple languages that use the same pages and strangely enough only the Title field seemed to have a common GUID across each of the languages and you can't use item["Title"] because they don't always match across languages (meaning Title is spelled differently in French or Spanish) only InternalName seemed to be constant across the languages. I could have used the index value but that is prone to change and is just bad practice.
Eli