I am creating a list using COM for Sharepoint 2010.
below is a snippet for the same:
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("myURL"), "NTLM", myCred);
// declare the ClientContext Object
SP.ClientContext _clientcontext = new SP.ClientContext(_context);
SP.Web _site = _clientcontext.Web;
// declare a ListCreationInfo
SP.ListCreationInformation _listcreationinfo = new SP.ListCreationInformation();
// set the Title and the Template of the List to be created
_listcreationinfo.Title = "NewListFromCOM";
_listcreationinfo.TemplateType = (int)SP.ListTemplateType.GenericList;
// Call the add method to the ListCreatedInfo
SP.List _list = _site.Lists.Add(_listcreationinfo);
// Add Description field to the List
for(int i=1;i<4;i++)
{
someStr = "<Field Type='Number' DisplayName='Col0" + i + "'></Field>";
SP.Field _Description = _list.Fields.AddFieldAsXml(@someStr, true, SP.AddFieldOptions.AddToDefaultContentType);
}
// declare the List item Creation object for creating List Item
SP.ListItemCreationInformation _itemcreationinfo = new SP.ListItemCreationInformation();
// call the additem method of the list to insert a new List Item
SP.ListItem _item = _list.AddItem(_itemcreationinfo);
_item["Title"] = "New Item from Client Object Model";
for (int i = 1; i < 4; i++)
{
someStr="Col0"+i+"";
_item[someStr] = 100 + i;
}
// call the update method
_item.Update();
I wanted to create columns like = Col01, Col02, Col03. Then create an item in the list, and update.
The list gets created successfully, but on updating I get an error:
Column 'Col01' does not exist. It may have been deleted by another user.
Could someone pls help why this is happening? The list exists, and I have tried deleting it and running the code again. It doesnt work. I have tried using the static name (="_x0043_ol01") for Col01 but can't figure out how.
Really appreciate your help.
_list.Update();
after the loop of adding fields