2

I'm trying to find a file in a folder by GUID. There is a reason behind it, because I get GUID in document set history and I cannot rely on a file name.

I'm stuck trying all the possible combinations of GUD/UniqueId for internal field name and possibilities for Value Type. If I search for a specific title, the 'same' code works.

What am I doing wrong? What id the proper internal field name and value type. If I search in UI for the file in a specified library by GUID used in code, it finds it, so there should not be problem in configuration.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SP = Microsoft.SharePoint.Client;
using DM = Microsoft.SharePoint.Client.DocumentSet;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String SharepointURL = "SP-URL";
            String DocLibName = "LibName";
            Guid itemGuid = new Guid("1156a28e-0c98-4c36-ac0a-fbc71d137dbd");

            SP.ClientContext context = new SP.ClientContext(SharepointURL);
            SP.Web web = context.Web;
            SP.List docLib = web.Lists.GetByTitle(DocLibName);
            context.Load(docLib);
            context.ExecuteQuery();
            SP.CamlQuery query = new SP.CamlQuery();

            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type=\"Text\">{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type=\"Guid\">{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type=\"Text\">{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View Scope='RecursiveAll'><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View Scope='RecursiveAll'><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type='Guid'>{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type=\"Guid\">{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type='Guid'>{0}</Value></Eq></Where></Query></View>", itemGuid);
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Contains><FieldRef Name='UniqueId'/><Value Type='Guid'>{0}</Value></Contains></Where></Query></View>", itemGuid);
            query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Contains><FieldRef Name='UniqueId'/><Value Type='Text'>{0}</Value></Contains></Where></Query></View>", itemGuid);

            ////This search is working
            //String value = "The test document";
            //query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", value);

            SP.ListItemCollection collListItem = docLib.GetItems(query);
            docLib.Context.Load(collListItem);
            docLib.Context.ExecuteQuery();

        }
    }
}
2
  • Check this if it helps. – Ganesh Sanap Jan 22 at 12:59
  • Did I misunderstand the article you are proposing? As far as I understand it does not search by GUID, it is using ID instead? I need to search by GUID which cannot be searched for in any other way, as far as I know, but using CamlQery – Furli Jan 22 at 13:23
2

there is no SPList.GetItemByUniqueId if you can use id try GetFileById(id)

If you can't use id In caml query use one of theses queries

Query 1

query.ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type='Guid'>{0}</Value></Eq></Where></Query></View>", itemGuid);

Query 2

query.ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type='Guid'>{0}</Value></Eq></Where></Query></View>", itemGuid)
1
  • I wish it would help. I have tried it - copy/paste bot of those, but both return 0 items. Is there a chance there is something wrong with configuration? I can even see index set on GUID in a library: You have created 3 of maximum 20 indices on this list. These indices are: Content Type GUID Modified By – Furli Jan 23 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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