2

I am using SharePoint Web Services to get some list items out of SharePoint for a project i am working on.

I am using using LINQ to XML to parse the resulting XML to be put into a datasheet. The problem i am running into is when trying to parse an item that isn't required in SharePoint...

var fields = from item in results.Descendants(XName.Get("row", "#RowsetSchema"))
                     select new
                     {
                         ID = item.Attribute("ows_ID").Value,
                         Title = item.Attribute("ows_Title").Value,
                         DNS = item.Attribute("ows_DNS_x0020_Name").Value
                     };

DNS Name is not a required item in the list and some items do not have an entry for this. the resulting xml from sharepoint omits the field from the XML causing a "Object reference not set to an instance of an object." exception.

is there a workaround for this without me having to put a where clause in the LINQ statement (just because there isn't a DNS Name entered does not mean that i don't want it to show up in the results)

TOSM
  • 25
  • 2
  • 7

2 Answers2

1
var fields = from item in results.Descendants(XName.Get("row", "#RowsetSchema")) 
             select new 
             { 
                ID = item.Attribute("ows_ID").Value, 
                Title = item.Attribute("ows_Title").Value, 
                DNS = item.Attribute("ows_DNS_x0020_Name") == null ? "" : item.Attribute.("ows_DNS_x0020_Name").Value 
             }; 

Wouldn't that work?

Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
  • unfortunately this doesn't work, I tried it yesterday after some looking and since the field isn't in the xml it throws the error when looking for the attribute. – TOSM Aug 11 '10 at 15:32
0

See my answer regarding adding a ViewFields parameter to your query here: Soapclient query a Sharepoint web service

You'd add a DNS_x0020_Name FieldRef to your particular ViewFields parameter.

Community
  • 1
  • 1
CBono
  • 3,803
  • 1
  • 32
  • 40
  • the item is in the viewfields xml but since it is not a required field in sharepoint if it isn't entered into the list item sharepoint omits the fieldref in the XML :( – TOSM Aug 11 '10 at 15:32