Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When running my SPSiteDataQuery (code below), I appear to be getting 1;# before the actual Author Name. Can anyone point me in the right direction towards an explanation to this please?

   SPSiteDataQuery query = new SPSiteDataQuery();

   query.Lists = @"<Lists ServerTemplate='850' Hidden='TRUE' />";
   query.ViewFields = @"<FieldRef Name=""Title"" /><FieldRef Name=""Author"" />";
   query.Webs = @"<Webs Scope='SiteCollection'/>";
   query.Query = @"<Where>
                   <BeginsWith>
                       <FieldRef Name='ContentTypeId'/>
                       <Value Type='Text'>[LONG GUID]</Value>
                   </BeginsWith>
                   </Where>";
   query.RowLimit = 10;

   DataTable dt = web.GetSiteData(query);

So in every dt I'll have: "1;#Daniel McNulty" in the Author column for example.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

When you query the content you are going to get it back in its actual format. For some fields like lookup fields and user fields the data type is actually multi-part and will use a format like this. If you are looking for the just the display name, just format the value the way you want it.

share|improve this answer
    
Thanks Mike. I don't have a problem substring'ing it, I just wanted to make sure there wasn't a more relevant field (somehow) with just the display name or that I wasn't being inefficient in any way. This is fine. Thanks. –  DeeMac Jun 18 '12 at 15:42
2  
Small point, make sure to parse it to characters after the #, as the ID (1 in this case) can be much greater than 9 and thus can take up a variable amount of characters. –  Zork Jun 18 '12 at 15:44
    
Thanks a lot Zork, noted. –  DeeMac Jun 18 '12 at 15:46
add comment

Author column uses an SPFieldLookup to the User Information List.

SharePoint 2007 and 2010 often use ;# as an internal splitter on their Data fields. SPSiteDataQuery returns the internal data values, Generally if you find a ;# there will be a built in Class that handles the conversion for you.

You can use the SPFieldLookupValue Class to handle the relevant string split.

string authorField = item["Author"].ToString();
SPFieldLookupValue authorValue = new SPFieldLookupValue(authorField);

// The account Login Name of the author 
authorValue.LookupValue
// The User Information List ID of the user
authorValue.LookupId

Depending on what you are trying to do the ID can be more useful than the Login Name.

For rendering the Datatable in a SPDataGrid you can use Item Templates to transform the values to a more useful format, E.G. A Link to the users profile.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.