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

 

THREE QUERIES offers easy access to information about SharePoint and associated technologies, project management, agile and scrum methodologies that helps developers, administrators, architects, technical managers, business analysts and end users. It has grown from there. We provide an important knowledge base for those involved in managing, architecture and developing software projects of all kinds. With weekly/daily exclusive updates, we keep you in touch with the latest business, management, technology thinking.

WE ARE CONNECTED ~ Follow us on social media to get regular updates and opinion on what's happening in the world of SharePoint, front-end, back end web technologies and project management. If you like this article, please share it and follow us at Facebook, Twitter, Instagram, Pinterest and LinkedIn

,

About Satyendra Mishra

Microsoft certified, motivated, energetic and accomplished SharePoint Consultant and Architect with 13+ years of work experience in Management, Architecture, Analytics, Development and Maintenance. He has been fortunate to be a part of over 20+ Web/Mobile/Software SharePoint and .Net projects delivery with various companies across different industry sectors. This has provided him a valuable insight and experience especially in successful implementation of technology solutions. Having very strong System Architecture, Technological, Management & Analytical Skills, Knowledge of Office 365, SharePoint, .Net, JavaScript, React, Angular, Azure, SQL. In 2016, he decided to start sharing the knowledge back to society in the form for content writing and blogging.
View all posts by Satyendra Mishra →

Leave a Reply

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