1

I want to display a list of items with name and modified time, like so:

  SPWeb thisWeb = SPContext.Current.Web;

  SPWebCollection webs = thisWeb.Webs;

  SPSiteDataQuery query = new SPSiteDataQuery();
  query.Lists = "<Lists BaseType=\"1\" />";
  query.ViewFields = 
      "<FieldRef Name=\"Modified\" Nullable=\"true\" />" +
      "<FieldRef Name=\"FileLeafRef\" Nullable=\"true\" />";

  DataTable table = thisWeb.GetSiteData(query);

  BoundField modified = new BoundField();
  modified.DataField = "modified";
  modified.HeaderText = "Modified";

  BoundField name = new BoundField();
  name.DataField = "FileLeafRef";
  name.HeaderText = "Namn";

  GridView.Columns.Add(name);
  GridView.Columns.Add(modified);

  GridView.DataSource = table;
  GridView.DataBind();

The resulting table looks like this:

1;#CSS       2012-06-12 08:58:45
7;#Images    2012-06-12 08:58:45

Can anyone tell me why Sharepoint is prepending what looks like the ID of the object?

The behaviour is especially preplexing because accessing the field via PowerShell looks ok:

> $web.Lists[1].Items[0]["FileLeafRef"]
  posten_intranet_base.css

2 Answers 2

1

Why? My best guess is... inside views SharePoint can use the ID to build a URL to the item.

To fix your issue, it will probably be easiest for you to just iterate over the DataTable rows and trim off the id;#.

3
  • Thank you for taking the time to answer. [ok, now I now that return sends the comment..] This behaviour strikes me as really unconvenient. Perhaps a better question would be "how would I go about achieving this without resorting to hackish methods like the one suggested in your answer?"
    – Jesper
    Commented Jun 13, 2012 at 17:27
  • Woops sorry, I didn't realize you had commented until now. Why is it a hack? If it works... it works. :P
    – Kit Menke
    Commented Jul 26, 2012 at 19:05
  • Yes, your solution works. I ended up using it. But my point is that the proper way to do it would be to give the programmer some way of accessing the value of a field directly, without the meta data.
    – Jesper
    Commented Jul 27, 2012 at 12:36
0

From the code available here it seems FileLeafRef column is a lookup column.

new SPFieldLookupValue(item["FileLeafRef"] as String).LookupValue

This will display only the text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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