5

Is it possible to get SP List Item by unique Id (without writing a Caml request) if I know a related List Id.

var item = list.GetItemById("CFA9E204-6509-424B-A246-0DE5295C42B2");

When I tried to get item using this code, I received an error: "Input string was not in a correct format."

What's wrong with the GetItemById() method?

Why does it has an overloaded method GetItemById() with a string argument if it understands only integer Id?

EDIT:

I use SharePoint Client Object Model where List entity has no a method GetItemByUniqueId(), but it has the GetItemById() method, which receives string or integer Id.

MSDN Documentation

Warlock
  • 7,321
  • 10
  • 55
  • 75

4 Answers4

4

Try this:

SPListItem item = list.GetItemByUniqueId(new Guid("CFA9E204-6509-424B-A246-0DE5295C42B2"));

Manish
  • 6,106
  • 19
  • 64
  • 90
  • Thanks, but this method is not presented in Microsoft.SharePoint.Client. I have updated question and provided reference on MSDN documentation. – Warlock Dec 19 '12 at 12:49
2

yes it's possible to load a listitem by it's unique identifier.you can use the SPList.GetItemByUniqueId method

see MSDN documentation on GetItemByUniqueId

Thorsten Hans
  • 2,675
  • 19
  • 21
  • Thanks, but this method is not presented in Microsoft.SharePoint.Client. I have updated question and provided reference on MSDN documentation. – Warlock Dec 19 '12 at 12:49
1

I was able to do it with following method, need your suggestions if its something looks workable

            CamlQuery cQuery = new CamlQuery();

            cQuery.ViewXml = string.Format(@"<View Scope='RecursiveAll'>
                                                      <Query>
                                                        <Where>
                                                          <Eq>
                                                            <FieldRef Name='UniqueId' />
                                                            <Value Type='Guid'>{0}</Value>
                                                          </Eq>
                                                        </Where>
                                                      </Query>
                                                    </View>", id);

            //ListItem item = listItem.GetItemById("293");

            ListItemCollection itemColl = listItem.GetItems(cQuery);

            sp.Context.Load(itemColl);

            sp.Context.ExecuteQuery();

            ListItem item = itemColl.FirstOrDefault();
sairfan
  • 970
  • 2
  • 12
  • 20
0

spliste.Items[<uniqueId>] should work.

For example:

Guid itemUniqueID = new Guid("bb49fd4e-c302-45ef-85dc-e12624423651");
SPListItem item = spliste.Items[itemUniqueID ];

If you want to get the content of a certain field of that item:

string content = spliste.Items[<uniqeId>][<fieldGuidOrDisplayName>].ToString();

For example:

Guid itemUniqueID = new Guid("bb49fd4e-c302-45ef-85dc-e12624423651");
string content = spliste.Items[itemUniqueID ]["Title"].ToString();