How to get a SharePoint List Item By Unique Id
Asked Answered
G

4

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

Grapheme answered 19/12, 2012 at 11:9 Comment(1)
Any luck getting the item using a guid?Otter
G
4

Try this:

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

Gloucester answered 19/12, 2012 at 12:6 Comment(1)
Thanks, but this method is not presented in Microsoft.SharePoint.Client. I have updated question and provided reference on MSDN documentation.Grapheme
G
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

Godwin answered 19/12, 2012 at 12:0 Comment(1)
Thanks, but this method is not presented in Microsoft.SharePoint.Client. I have updated question and provided reference on MSDN documentation.Grapheme
L
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();
Linnie answered 12/10, 2018 at 22:9 Comment(0)
B
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();
Betel answered 1/11, 2013 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.