Hi Guys and Gals,
I've found myself in a situation where I need to change some of the promoted fields in an infopath library. I've started writing a simple console app in order to change these fields. According to a number of blog articles I've read it is possible to change the promoted fields in the sharepoint database and the created by field with c#. I've managed to change the created date programmatically with code that looks a bit like this:
SPSite site = new SPSite("http://MyWebsiteURL");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyLibraryName"];
SPListItemCollection collection = list.Items;
SPUser user = web.EnsureUser("LoginNameOfUserToChangeTo");
foreach (SPListItem item in collection )
{
//1073741823;#System Account //User name values are in this format ID;#Login Name
string AccountIDString = user.ID + ";#" + user.Name; //Create in same format
SPSecurity.RunWithElevatedPrivileges(delegate()
{
item["Created"] = new DateTime(2008, 5, 7);
item["Author"] = AccountIDString;
item.Update(); //Update the item
}
);
}
// Update list then site
list.Update();
web.Update();
Strangely enough the code runs fine and throws no errors but most properties will not update. The only indexed column which changes is the Created field which contains the date the document was created. I've tried updating the promoted fields from the InfoPath form itself but no joy. I've tried referencing the item as item[Created By] etc.
I can open the library in datasheet view but it shows as read-only. I've even given myself full control over the library, still nada. I can even write the values of the fields with Console.Write. The update, however doesn't work. strangely enough also, I am able to update the list and give the library a new name programmatically, I can even rename the site programmatically using the code above but I can't change the promoted properties or the created by field. Any ideas?