SharePoint Web services Samples


//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 = " " +
"" + ID + "" + Title+ "";
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 = "" + ListID+ "" + Title + "";

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 = "" + ID +"";

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)

Below is the code to get the values of a choice field in a sharepoint list . You can save these values into a datatable and can bind the datatable with a Drop-down control.

//get choice field values from sharepoint list 

protected void GetDrpDownValuesFromList()
{
SPWebservice.Lists FMService = new SPWebservice.Lists();
FMService.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode ndLists = FMService.GetList("MyList"); //add list name
XmlDocument doc = new XmlDocument();
doc.LoadXml(ndLists.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 FieldsInList = doc.SelectNodes("//sp:Field", mg);

foreach (XmlNode Field in FieldsInList)
{
if (Field.HasChildNodes)
{
if (Field.Attributes["Name"].Value == "choiceFieldName")
{
foreach (XmlNode node in Field.ChildNodes)
{
if (node.HasChildNodes)
{
foreach (XmlNode Newnode in node.ChildNodes)
{
if(Newnode.HasChildNodes)
{
_addToDataTable(Newnode.InnerText); // Add node value to datatable
}}}}}}}

0 comments:

Post a Comment