Friday, February 6, 2009

Geting SharePoint web service response in DataTable c#

Hi all,
Few days before I was exploring how to get SharePoint web service response in DataTable instead of XMLNode?. for example you are using SharePoint List.asmx web service and try to get all SharePoint List collection get this response in DataTable.
here is code snippet.


/// <summary>
/// Get SharePoint List collections using SharePoint web service
/// Instead of Xml response it return DataTable.
/// remove XML name space from web service response.
/// </summary>
/// <returns></returns>
public DataTable GetSharePointListCollection()
{
try
{
//ListService is SharePoint List.asmx List Instance.
XmlNode ndSPLists = ListService.GetListCollection();
//to remove xml Name spance.
Regex StripXmlnsRegex =new Regex(@"(xmlns:?[^=]*=[""][^""]*[""])",RegexOptions.IgnoreCase|RegexOptions.Multiline|RegexOptions.Compiled| RegexOptions.CultureInvariant);

//to find and replace short XmlNameSpace like z:,rs: from xmlresponse by GetListItems method
Regex removeShortNameSpace = new Regex(@"(?<lessthan><)(?<closetag>[/])?(?<shortname>\w+:)\s*", RegexOptions.IgnoreCase|RegexOptions.Multiline|RegexOptions.Compiled|RegexOptions.CultureInvariant);

//remove namespace xmlnls from result xml..
string xmlResponse = StripXmlnsRegex.Replace(ndSPLists.InnerXml, "");

//find and replace short XmlNameSpace like z:,rs: from responce with space..
xmlResponse = removeShortNameSpace.Replace(xmlResponse, delegate(Match m)
{
Group closetag = m.Groups["closetag"];
if (closetag.Length != 0)
{
return "</";
}
else
{
return "<";
}

});
//load xml from removed XmlNameSpace and short name of XmlNameSpace..
//XDocument xmlDoc = new XDocument();
//xmlDoc = XDocument.Parse(xmlResponse);
xmlResponse = "<Lists>" + xmlResponse + "</Lists>";
XDocument xdoc = XDocument.Parse(xmlResponse);

using (StringReader reader = new StringReader(xdoc.ToString()))
{
DataSet dsSPLists = new DataSet();
dsSPLists.ReadXml(reader);
if (dsSPLists.Tables.Count > 0)
{
return dsSPLists.Tables[0];
}
}

return null;
}
catch
{
throw;
}
}

4 comments:

Anonymous said...

Thanks for posting this it helped me a lot. I was using this code for several weeks until I found the example below (can't remember where though).

ndSPLists = service.GetListItems(listName, null, ndQuery, ndFields, null, ndQueryOptions, null);

DataSet ds = new DataSet();
StringReader sr = new StringReader(ndSPLists.OuterXml);

ds.ReadXml(sr);

DataSet hereItIs = ds.Tables["Row"];

Rajeev Elanthoor said...

Good one !!!

Anonymous said...

It appears that it would work but can you explain "ListService.GetListCollection();" - where are you getting that from? A namespace? A web service?

SharePoint Kings said...

if you checked comment just about taking reference it says

//ListService is SharePoint List.asmx List Instance.

so its object of SharePoint List.asmx web service.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com