How to Consume SharePoint XML web service in .NET Visual Studio Projects

There are two ways to get the reference of XML web service in .NET Visual Studio project:

  1. Add web reference to the project or
  2. Create proxy class using WSDL.exe and include it in Visual Studio project

I am following step 2 here.

To make proxy class generic add a constructor in proxy class with a parameter as web url

public Lists(string contentHubUrl)
{
this.Url = string.Format(“{0}/_vti_bin/lists.asmx”, contentHubUrl);
}

Now in your code write a method that will consume this proxy class and fetch the relevant items form SharePoint list.




private DataTable GetLists(string url, string listName, string[] columnNames, string login, string password, string queryText, string filterFields)
{
DataTable dtOutput = new DataTable();
//List WebService
Lists ls = new Lists(url);
ls.PreAuthenticate = true;
ls.Credentials = new NetworkCredential(login, password);
ls.Url = url + @”/_vti_bin/lists.asmx”;

            XmlNode node = ls.GetListCollection();
            foreach (XmlNode list in node.ChildNodes)
{
string title = list.Attributes[“Title”].Value;

                if (title.Equals(listName))
{
XmlDocument xmlDoc = new System.Xml.XmlDocument();

                    XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, “Query”, “”);
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, “ViewFields”, “”);
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element, “QueryOptions”, “”);

                    ndQueryOptions.InnerXml = string.Empty;
ndViewFields.InnerXml = filterFields;
ndQuery.InnerXml = queryText;

                    //Getting Items
                    XmlNode itemsNode = ls.GetListItems(title, string.Empty, ndQuery, ndViewFields, null, ndQueryOptions, list.Attributes[“WebId”].Value);
                    using (XmlTextReader reader = new XmlTextReader(itemsNode.OuterXml, XmlNodeType.Element, null))
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(reader);

                        dtOutput.Columns.Add(“Title”, typeof(string));
                        foreach (var columnName in columnNames)
{
dtOutput.Columns.Add(columnName, typeof(string));
}

                        if (dataSet.Tables[“row”] != null)
{
foreach (DataRow row in dataSet.Tables[“row”].Rows)
{
DataRow dr = dtOutput.NewRow();
dr[“Title”] = (row[“ows_Title”] != null && !string.IsNullOrEmpty(row[“ows_Title”].ToString())) ? row[“ows_Title”].ToString() : string.Empty;

                                foreach (var columnName in columnNames)
{
string replacedColumnName = columnName.Replace(“_x0020_”, ” “);

                                    dr[columnName] = (row[“ows_” + replacedColumnName] != null &&
!string.IsNullOrEmpty(row[“ows_” + replacedColumnName].ToString()))
? row[“ows_” + replacedColumnName].ToString()
string.Empty;
}
dtOutput.Rows.Add(dr);
}
}
dataSet.Dispose();
}
break;
}
}

            ls.Abort();
return dtOutput;
}




Call above method as below:

  string[] showFields = new string[2];
showFields[0] = “ID”;
showFields[1] = “EventDate”;
DataTable eventsTable = this.GetLists(WebURL, ListName, showFields, “userName”,  “password”, [CAML QUERY] as string, “”);

Above code is to fetch the items from List. Now below is the code to update list item

private bool UpdateListItem(string url, string listName, string columnName, string titleText, string newColumnValue, string login, string password)
{
bool isUpdated = false;
//List WebService
Lists ls = new Lists();
ls.PreAuthenticate = true;
ls.Credentials = new NetworkCredential(login, password);
ls.Url = url + @”/_vti_bin/lists.asmx”;

            XmlNode node = ls.GetListCollection();
            foreach (XmlNode list in node.ChildNodes)
{
////Check whether list is document library
//if (Convert.ToInt32(list.Attributes[“ServerTemplate”].Value) != 0x65)
//{
//    continue;
//}

                string title = list.Attributes[“Title”].Value;
                if (title.Equals(listName))
{

                    XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, “Query”, “”);
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, “ViewFields”, “”);
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element, “QueryOptions”, “”);

                    ndQueryOptions.InnerXml = string.Empty;
ndViewFields.InnerXml = “” + newColumnValue + “”;

                                XmlDocument xmlDocToUpdate = new System.Xml.XmlDocument();
                                System.Xml.XmlElement elBatch = xmlDocToUpdate.CreateElement(“Batch”);
                                elBatch.SetAttribute(“OnError”, “Continue”);
elBatch.SetAttribute(“ListVersion”, “1”);
elBatch.SetAttribute(“ViewName”,
string.Empty);
elBatch.InnerXml = strBatch;
XmlNode ndReturn = ls.UpdateListItems(title, elBatch);
isUpdated = true;
}
}

                        dataSet.Dispose();
}
break;
}
}
ls.Abort();
return isUpdated;
}

Call above method using

isDone = this.UpdateListItem(destinationListWebUrl, destinationListName, destinationListColumnName, dr[“Title”].ToString().Replace(“&”, “&”), finalColumnValue, userID, pwd);




Be Connected…

Satyendra

,

About Satyendra Mishra

Satyendra is a Technical Project Manager with 12+ years of experience on Project Management, Microsoft Technologies including ASP.NET, ASP.NET MVC, C#, SharePoint (On premises & Office 365), JavaScript Framework (AngularJS) and related technologies. He is a person with belief, sky is limit for a hard working and dedicated individual. That’s why he devoted himself to field of Software Development and now both are growing with each other. Every year for him turned out to be best year and he received enormous support and love from his companies and his clients from around the world. Now with vast knowledge of software development and its outsourcing process, he decided to move himself into knowledge sharing. Keep a fact in your mind about the comfort level of your clients and this will make you king/queen of your area.
View all posts by Satyendra Mishra →

Leave a Reply

Your email address will not be published. Required fields are marked *