SharePoint Web services Examples

//Add a New List Item
protected void CreateListItem(string ID, string Title)
{

SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument doc = new XmlDocument();

System.Xml.XmlElement batch = doc.CreateElement("Batch");

batch.SetAttribute("OnError", "Continue");

batch.SetAttribute("ListVersion", "1");

batch.SetAttribute("ViewName", strViewID);

batch.InnerXml = "<method> <id="1" cmd="New">" +
"<field name="Id">" + ID + "</field><field name="Title">" + Title+ "</field>";
try
{
SPService.UpdateListItems(strListID, batch);
}
catch { }

}

//Update list Item
protected void UpdateListItem(string ListID,string Title)
{
SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument doc = new XmlDocument();

System.Xml.XmlElement batch = doc.CreateElement("Batch");

batch.SetAttribute("OnError", "Continue");

batch.SetAttribute("ListVersion", "1");

batch.SetAttribute("ViewName", strViewID);

batch.InnerXml = "<method id="1" cmd="Update"><field name="ID">" + ListID+ "</field><field name="Title">" + Title + "</field></method>";

try
{
SPService.UpdateListItems(strListID, batch);
}
catch { }

}

//Get List Items
protected void GetListItems(string ID)
{

SpWebservice.Lists SPService = new SpWebservice.Lists();
SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;

System.Xml.XmlNode ndListView = SPService.GetListAndView("MyList", "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

ndViewFields.InnerXml = "";

ndQuery.InnerXml = "<Where><Eq><FieldRef Name=ID/><Value Type='Number'>" + ID +"</Value></Eq></Where>";

try
{
XmlNode ndListItems =
SPService.GetListItems(strListID, null, ndQuery, ndViewFields, null, null, null);

XmlDocument doc = new XmlDocument();
doc.LoadXml(ndListItems.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");

XmlNodeList NodeList = doc.SelectNodes("//sp:listitems/rs:data", mg);

foreach (XmlNode ListItem in NodeList)
{
foreach (XmlNode node in ListItem.ChildNodes)
{
string ID = string.Empty;
string Title = string.Empty;

XmlAttribute id = node.Attributes["ows_ID"];

if (id != null)
{
ID = id.Value;
}

XmlAttribute _Title= node.Attributes["ows_Title"];

if (_Title!= null)
{
Title= _Title.Value;
}

_AddToDataTable(ID, Title);
}
}
catch { }

}

// Get list Fields (using GetList method)

Please see my Post Here to get values in a choice field column in a list.

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com