Sunday, November 16, 2008

How to iterate SharePoint List Fields using SharePoint web service?

How to iterate SharePoint List Fields using SharePoint web service?
Hi All,
Here is some code snippet that will help you out to get rid of it.
Here is a scenario where I need to iterate fields and populate these fields in dropdown list for selected SharePoint list in dropdown list.
Reference from link http://www.codeguru.com/csharp/csharp/cs_webservices/tutorials/print.php/c8805__2/

private XmlNodeList RunXPathQuery(XmlNode XmlNodeToQuery, string XPathQuery)
{
// load the complete XML node and all its child nodes into an
// XML document
XmlDocument Document = new XmlDocument();

Document.LoadXml(XmlNodeToQuery.OuterXml);


// all the possible namespaces used by SharePoint and a randomly
// choosen prefix
const string SharePointNamespacePrefix = "sp";
const string SharePointNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/";
const string ListItemsNamespacePrefix = "z";
const string ListItemsNamespaceURI = "#RowsetSchema";
const string PictureLibrariesNamespacePrefix = "y";
const string PictureLibrariesNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/ois/";
const string WebPartsNamespacePrefix = "w";
const string WebPartsNamespaceURI =
"http://schemas.microsoft.com/WebPart/v2";
const string DirectoryNamespacePrefix = "d";
const string DirectoryNamespaceURI =
"http://schemas.microsoft.com/sharepoint/soap/directory/";
const string DataRowSetNameSpacePrefix = "rs";
const string DataRowSetNameSpaceURI = "urn:schemas-microsoft- com:rowset";

// now associate with the xmlns namespaces (part of all XML
// nodes returned from SharePoint), a namespace prefix that
// we then can use in the queries
XmlNamespaceManager NamespaceMngr =
new XmlNamespaceManager(Document.NameTable);
NamespaceMngr.AddNamespace(SharePointNamespacePrefix,
SharePointNamespaceURI);
NamespaceMngr.AddNamespace(ListItemsNamespacePrefix,
ListItemsNamespaceURI);
NamespaceMngr.AddNamespace(PictureLibrariesNamespacePrefix,
PictureLibrariesNamespaceURI);
NamespaceMngr.AddNamespace(WebPartsNamespacePrefix,
WebPartsNamespaceURI);
NamespaceMngr.AddNamespace(DirectoryNamespacePrefix,
DirectoryNamespaceURI);
NamespaceMngr.AddNamespace(DataRowSetNameSpacePrefix, DataRowSetNameSpaceURI);

// run the XPath query and return the result nodes
return Document.SelectNodes(XPathQuery, NamespaceMngr);
}

When you click button you need to populate fields of “MySharePoint List” to dropdown list.
protected void Button1_Click(object sender, EventArgs e)
{
//List Web service instance.
ListService.Lists listService = new ListService.Lists();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("admin", "admin");
System.Net.CredentialCache cache=new System.Net.CredentialCache();
cache.Add(new Uri("http://localhost:80"),"NTLM",cred);
listService.Credentials=cache;
listService.Url = "http://localhost:80/_vti_bin/lists.asmx";
//Provide SharePoint List name
XmlNode list = listService.GetList("MySharePointList");
//here “//sp:Field” is XPath query we need to pass.
XmlNodeList nodes = RunXPathQuery(list, "//sp:Field");
foreach (XmlNode node in nodes)
{
try
{
bool hidden = false;
bool ReadOnly = false;
string ID = node.Attributes["ID"].Value;
string Name = node.Attributes["Name"].Value;
try{hidden=Convert.ToBoolean(node.Attributes["Hidden"].Value);}catch{}
try { ReadOnly = Convert.ToBoolean(node.Attributes["ReadOnly"].Value); }
catch { }
if (!string.IsNullOrEmpty(ID) && !string.IsNullOrEmpty(Name))
{
if(!hidden || !ReadOnly)
//ddlField is dropdown list which we need to populate.
ddlField.Items.Add(new ListItem(Name, ID));
}
}
catch { }

}
}

1 comment:

Anonymous said...

Thanks for this post. I was able to get this to work. I had to make one small change though. I think it might be because I was passing a query into the .GetList() method and returning only 1 record. so the XPath Expression "//sp:Field" always returned 0. I had to change it to "//z:row" and I was able to get it. and instead of node.Attribute("Name").Value i had to use node.Attribute("ows_Name").value

other than that this worked great. Exactly what I needed and have been trying to figure out for months now. Thanks.




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