There are two ways to get the reference of XML web service in .NET Visual Studio project:
- Add web reference to the project or
- 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
Hope you enjoyed reading it.
Please don't forget to Share, Follow and Like to get updated with latest posts.
It gives me motivation to share more knowledge with you.
About Author
Satyendra Mishra
Project Management Professional (PMP) and Microsoft certified, motivated, energetic and accomplished Project Manager / Architect with 15+ years of work experience in Management, Architecture, Analytics, Development and Maintenance. I have been fortunate to be a part of over 25+ .Net / SharePoint projects delivery with various companies across different industry sectors. This has provided me a valuable insight and experience especially in successful implementation of SharePoint based solutions.
My experience in Web application implementation includes technology strategy and road-map definition, business and technical requirements identification, governance, platform architecture, solution design, configuration, development, quality assurance, training, post-production support, team lead and overall project delivery along with project management.
Satyendra Mishra holds a B.Tech. in Computer Science & Engineering and PG Diploma in Advance Computing from Center for Development and Advance Computing, Pune, India. He is also a certified Project Management Professional (PMP).
I love to share Project Management Tips and Tricks by writing Blogs in the Project Management India Community. I have written around 300+ articles in the Project Management, .Net, SharePoint and related client side technologies.