3

What is the best way to get a list item Title by using it's GUID, I can only think about opening web list resides then list and at last list.getitembyid...

Any better way of doing it, it has to be done in c# ?

4
  • 1
    is there any specific reason why you need to retrieve the item by GUID instead of by ID ?
    – MdMazzotti
    May 18, 2015 at 9:22
  • Please be more specific, specify what API at least.. May 18, 2015 at 9:37
  • @VadimGremyachev did i not mentioned c# ? server side API to be more specific does that have sub types as well ? would you like to share if you know any sub types please :) May 18, 2015 at 9:38
  • Thanks :) I was only interested to know whether it is SSOM or not May 18, 2015 at 9:40

2 Answers 2

4

You coud use the SPList.GetItemByUniqueId method method like so:

using (SPSite site = new SPSite(url))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists[listTitle];
        SPListItem item = list.GetItemByUniqueId(itemGuid);
    }
}

or you could use a CAML query like so:

SPList list = web.Lists["listTitle"];

SPQuery spQuery = new SPQuery();
spQuery.RowLimit = Convert.ToUInt32(list.ItemCount); 
String query = "<Where><Eq><FieldRef Name="GUID"/><Value Type="GUID">INSERT GUID HERE</Value></Eq></Where>";
spQuery.Query = query;

SPListItemCollection spListItemCollection = list.GetItems(spQuery);
1

Your CAML is broken because fieldref name should be UniqueId. here is the fixed code.

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {Add-PSSnapin Microsoft.SharePoint.PowerShell}

$web=get-SPWeb "https://portal.SPSite.com/sites/docs"
$list = $web.Lists["Shared Documents"]

$CAML = '<Where><Eq><FieldRef Name="UniqueId"/><Value Type="GUID">797237DF-C6F6-4031-AAE5-4525D5FF98F1</Value></Eq></Where>'
$query = new-object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='Recursive'"
$query.Query = $CAML
$listitems = $list.GetItems($query)
$count = $listitems.count
write-host "count: "$count
if ($count -ge 1)
{
    foreach($item in $listitems)
    {
        write-host $item["Title"] + " - " +  $item["Name"] 

    }
}

$web.dispose()

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.