Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Graph API folder exists

I am using Microsoft Graph API and I am creating a folder like so:

var driveItem = new DriveItem
{
    Name = Customer_Name.Text + Customer_LName.Text,
    Folder = new Folder
    {
    },
    AdditionalData = new Dictionary<string, object>()
    {
        {"@microsoft.graph.conflictBehavior","rename"}
    }
};

var newFolder = await App.GraphClient
  .Me
  .Drive
  .Items["id-of-folder-I-am-putting-this-into"]
  .Children
  .Request()
  .AddAsync(driveItem);

My question is how do I check if this folder exists and if it does get the id of the folder?

like image 487
user979331 Avatar asked Dec 03 '19 15:12

user979331


2 Answers

Graph API provides a search facility, that you could utilise to find out if an item exists. You have either an option of running the search first and then creating an item if nothing was found, or you could do as @Matt.G suggests and play around nameAlreadyExists exception:

        var driveItem = new DriveItem
        {
            Name = Customer_Name.Text + Customer_LName.Text,
            Folder = new Folder
            {
            },
            AdditionalData = new Dictionary<string, object>()
            {
                {"@microsoft.graph.conflictBehavior","fail"}
            }
        };

        try
        {
            driveItem = await graphserviceClient
                .Me
                .Drive.Root.Children
                .Items["id-of-folder-I-am-putting-this-into"]
                .Children
                .Request()
                .AddAsync(driveItem);
        }
        catch (ServiceException exception)
        {
            if (exception.StatusCode == HttpStatusCode.Conflict && exception.Error.Code == "nameAlreadyExists")
            {
                var newFolder = await graphserviceClient
                    .Me
                    .Drive.Root.Children
                    .Items["id-of-folder-I-am-putting-this-into"]
                    .Search(driveItem.Name) // the API lets us run searches https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=csharp
                    .Request()
                    .GetAsync();
                // since the search is likely to return more results we should filter it further
                driveItem = newFolder.FirstOrDefault(f => f.Folder != null && f.Name == driveItem.Name); // Just to ensure we're finding a folder, not a file with this name
                Console.WriteLine(driveItem?.Id); // your ID here
            }
            else
            {
                Console.WriteLine("Other ServiceException");
                throw;// handle this
            }
        }

The query text used to search for items. Values may be matched across several fields including filename, metadata, and file content.

you can play with search query and do things like filename=<yourName> or potentially examine file types (which i guess is not going to help in your particular case, but I'd mention it for completeness sake)

like image 131
timur Avatar answered Nov 05 '22 08:11

timur


To get the folder with the folder name:

call graph api Reference1 Reference2: /me/drive/items/{item-id}:/path/to/file

i.e. /drive/items/id-of-folder-I-am-putting-this-into:/{folderName}

  • If the folder exists it returns a driveItem Response, which has the id

  • If the folder doesn't exist, it returns a 404 (NotFound)

Now, while creating a folder, if the folder already exists, in order to fail the call, try setting additional data as follows Reference:

    AdditionalData = new Dictionary<string, object>
    {
        { "@microsoft.graph.conflictBehavior", "fail" }
    }
  • This will return a 409 Conflict, if the folder exists
like image 5
Matt.G Avatar answered Nov 05 '22 07:11

Matt.G