TOSM
TOSM

Reputation: 25

LINQ to XML problem with SharePoint Web Services

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)

Upvotes: 2

Views: 706

Answers (2)

Richard Anthony Hein
Richard Anthony Hein

Reputation: 10650

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?

Upvotes: 1

CBono
CBono

Reputation: 3803

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.

Upvotes: 0

Related Questions