views:

36

answers:

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)

A: 

See my answer regarding adding a ViewFields parameter to your query here: http://stackoverflow.com/questions/3447152/soapclient-query-a-sharepoint-web-service/3453380#3453380

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

CBono
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
+1  A: 
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 Hein
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