Posted by: R Manimaran | July 30, 2010

SharePoint List Operation using SharePoint Web Services – Part2

In the part1 I we have seen how  to create a new list and add columns to the list. Here we are going to add some values, add the columns to the default view, and update an item

Add new Columns to the Default View:

//Get the Default View of the List
XmlNode xnDefaultView = oViews.GetView(listName,“”);
string sDefaultViewName = xnDefaultView.Attributes[“Name“].Value.ToString();
XmlNode xnViewFields = xmlDoc.CreateNode(XmlNodeType.Element,”ViewFields“,“”);
//Get the already exists default view columns
foreach(XmlNode childNode in xnDefaultView.ChildNodes)
{
if(childNode.Name==”ViewFields“)
{
xnViewFields.InnerXml +=childNode.InnerXml;
}
}
//Add the newly added fields to the View Fields
xnViewFields.InnerXml +=
“<FieldRef Name = ‘Employee Name’/>” +
“<FieldRef Name = ‘Designation’/>”+
“<FieldRef Name = ‘Years of Exp’/>”;
oViews.UpdateView(listName,sDefaultViewName,null,null,xnViewFields,null,null,null);

Add a List Item to the newly created List:

XmlNode xnEmployees = oLists.GetListItems(listName, null, null, null, null, null, null);

XmlDocument xBatchdoc = new XmlDocument();

XmlElement xBatchElement = xBatchdoc.CreateElement(“Batch“);

xBatchElement.SetAttribute(“OnError“, “Continue”);

xBatchElement.SetAttribute(“ListVersion“, “1“);

xBatchElement.SetAttribute(“ViewName“, “”);

xBatchElement.InnerXml =

“<Method ID =’1′ Cmd = ‘New’>” +

“<Field Name = ‘Title’>11277</Field>” +

“<Field Name = ‘Employee_x0020_Name’>Maran</Field>” +

“<Field Name = ‘Designation’>Senior Engineer</Field>” +

“<Field Name = ‘Years_x0020_of_x0020_Exp’>1</Field>” +

“</Method>”;

XmlNode xncreatedList= oLists.UpdateListItems(listName, xBatchElement);

Update a previously added ListItem:

XmlNode xnQuery = xmlDoc.CreateNode(XmlNodeType.Element, “Query”,””);

XmlNode xnViewFields1 = xmlDoc.CreateNode(XmlNodeType.Element, “ViewFields”, “”);

XmlNode xnQueryOption = xmlDoc.CreateNode(XmlNodeType.Element, “QueryOptions”, “”);

xnEmployees = oLists.GetListItems(listName, null,xnQuery, xnViewFields1, null, xnQueryOption, null);

XmlNamespaceManager nsMgr= new XmlNamespaceManager(xmlDoc.NameTable);

nsMgr.AddNamespace(“z”,”#RowsetSchema”);

nsMgr.AddNamespace(“rs“,”urn:schemasmicrosoftcom:rowset“);

XmlNodeList nodes = xnEmployees.SelectNodes(“/rs:data/z:row/@ows_ID“, nsMgr);

string Id = string.Empty;

foreach (XmlNode node in nodes)

{

Id = node.InnerXml.ToString();

break;

}

XmlElement xBatchUpdateElement = xBatchdoc.CreateElement(“Batch“);

xBatchUpdateElement.SetAttribute(“OnError“, “Continue“);

xBatchUpdateElement.SetAttribute(“ListVersion“, “1“);

xBatchUpdateElement.SetAttribute(“ViewName“, “”);

xBatchUpdateElement.InnerXml =

“<Method ID =’1′ Cmd = ‘Update’>” +

“<Field Name =’ID’>”+Id+”</Field>”+

“<Field Name = ‘Title’>11277</Field>” +

“<Field Name = ‘Employee_x0020_Name’>Maran_Updated</Field>” +

“<Field Name = ‘Designation’>Senior Engineer – Technology </Field>” +

“<Field Name = ‘Years_x0020_of_x0020_Exp’>1</Field>” +

“</Method>”;

XmlNode xncreatedList1 = oLists.UpdateListItems(listName, xBatchUpdateElement);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: