Parsing Field Values from Sharepoint List Services Lists.GetList
I'm trying to write something that quickly will grab field values (e.g. combo box, lookups, etc) using the Sharepoint Web Services. The following code works, but is slow and seems inefficient. Is there any way to turn this into a LINQ style query with XDocument/XElement? When I try to Parse
the OuterXml it seems to load incorrectly.
MSDN - Lists.GetList
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument doc = new XmlDocument();
doc.LoadXml(ProuductionResultNode.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
XmlNodeList FieldsInList = doc.SelectNodes("//sp:Field", mg);
foreach (XmlNode Field in FieldsInList)
{
if (Field.HasChildNodes)
{
if (Field.Attributes["Name"].Value == fieldNameInternal)
{
foreach (XmlNode node in Field.ChildNodes)
{
if (node.HasChildNodes)
{
foreach (XmlNode Newnode in node.ChildNodes)
{
if (Newnode.HasChildNodes)
{
ret.Add(Newnode.InnerText);
}
}
}
}
}
}
}
return ret;
An example dropdown Field looks like this:
<Field Type="Choice" DisplayName="Media Type" Required="FALSE" Format="Dropdown" FillInChoice="FALSE" ID="{d814daf1-0bd2-48cc-8709-a513a3de4ef4}" SourceID="{1c01c034-f1fd-447f-8ed4-d60b997d0c3a}" StaticName="Media_x0020_Type" Name="Media_x0020_Type" ColName="nvarchar4" RowOrdinal="0" Version="3"><Default>CD/DVD</Default><CHOICES><CHOICE>CD/DVD</CHOICE><CHOICE>Hard Drive</CHOICE><CHOICE>Flash Drive</CHOICE><CHOICE>Virtual</CHOICE></CHOICES></Field>
The other queries I am using for GetListItems seems to work beautfiully.
XElement ziprecords = XElement.Parse(ZipItemsResultNode.OuterXml);
XName name = XName.Get("data", "urn:schemas-microsoft-com:rowset");
var iterationNotes =
from ele in ziprecords.Element(name).Elements()
where ele.Attribute("ows_Title").Value.Contains(fileName
where ele.Attribute("ows_Iteration_x0020_Notes") != null
select new { iterationNote = ele.Attribute("ows_Iteration_x0020_Notes").Value,
fileName = ele.Attribute("ows_Title").Value };
My Solution
I came up with a solution after some searching. I'm not sure why regular Parse
and other methods didn't work, but this seems to fix whatever mistakes I was making. I"ll leave this here incase somebody can explain why these steps are needed. I suspect there was an issue with the encoding, which I assumed XDcoument/etc would naturally take care of.
ProuductionResultNode = listservice.GetList(productiontable_listGUID);
XmlDocument xdoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("ans", "http://schemas.microsoft.com/sharepoint/soap/");
byte[] byteArray = Encoding.ASCII.GetBytes(ProuductionResultNod开发者_JAVA百科e.SelectSingleNode(".//ans:Fields", nsmgr).OuterXml);
MemoryStream stream = new MemoryStream(byteArray);
XElement xe = XElement.Load(stream);
XElement qry =
(from field in xe.Descendants()
where field.Attribute("Name") != null
where field.Attribute("Name").Value == "Ship_x0020_Via"
select field).Single();
List<string> ret = new List<string>();
foreach (XElement xle in qry.XPathSelectElements(".//ans:CHOICES", nsmgr).Elements())
{
ret.Add(xle.Value);
}
精彩评论