2

how to make this real using just a given Url and whether it's possible or not, idk?

what i am trying to do :

Creating Folder in specific place in the Drive according to a String..
this String consist from 3 parts (every part represents folder easy!) for instance, mystring = "Analyse_General_Theory" , the path in Drive should be like : Analyse/General/Theory

so :

my Imagination to Solution would be like that :)

passing my stringUrl to Build Request then Posting my Folder

stringUrl = "https://CompanyDomin.sharepoint.com/sites/mySite/SharedFolders/Analyse/General/Theory"

then

await graphClient.Request(stringUrl).PostAsync(myLastFolder) !!! 

so that would be the result !

Analyse/General/Theory/myLastFolder

is there somethig like that ? or maybe similar to this Approach ?

Shama
  • 55
  • 1
Hasan Othman
  • 68
  • 1
  • 10

1 Answers1

7

If you want to use Graph API to create a folder in SharePoint, please use the following Microsoft graph Rest API. Because Azure AD graph API just can be used to manage Azure AD resources (such as user, group, etc) and cannot be used to manage SharePoint resources. If we want to manage SharePoint resources with Graph API, we need to use Microsoft Graph API

POST https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{parent-item-id}/children

For example

POST https://graph.microsoft.com/v1.0/sites/CompanyDomin.sharepoint.com/drive/items/root:/
{folder path}:/children

{
  "name": "<the new folder name>",
  "folder": { },
  "@microsoft.graph.conflictBehavior": "rename"
}

Regarding how to implement it with SDK, please refer to the steps are as below

  1. Register Azure AD application

  2. Create a client secret.

  3. Add API permissions for the application. Please add Application permissions : Files.ReadWrite.All and Sites.ReadWrite.All.

  4. Code. I use Client credential flow.

/* please run the following command install sdk Microsoft.Graph and Microsoft.Graph.Auth 

   Install-Package Microsoft.Graph
   Install-Package Microsoft.Graph.Auth -IncludePrerelease

*/

 string clientId = "<your AD app client id>";
            string clientSecret = "<your AD app client secret>";
            string tenantId = "<your AD tenant domain>";
            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                        .Create(clientId)
                        .WithTenantId(tenantId)
                        .WithClientSecret(clientSecret)
                        .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);
            var item = new DriveItem
            {

                Name = "myLastFolder",
                Folder= new Folder { },
                AdditionalData = new Dictionary<string, object>()
                    {
                        {"@microsoft.graph.conflictBehavior","rename"}
                    }
            };
            var r = await graphClient.Sites["<CompanyDomin>.sharepoint.com"].Drive.Items["root:/Analyse/General/Theory:"].Children.Request().AddAsync(item);
            Console.WriteLine("the folder name : " + r.Name);

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • i did it , but you know ,i got this famous Exception which i couldn't dealing with it .. when i restart the app without signout and trying to create another folder in sharepoint , the expection happens and the InnerException says ("no account or login hint was passed to the AcquireTokenSlient Call"), i searched and found it's about the conficts of values (app doesn't make authorization call to fetch the new Acces Token, and that's because Cache disapperad once i closed the app , but cookies still a live , Now how to tell him i need new Aktion token in every call without clearing the cookies) – Hasan Othman May 07 '20 at 10:52
  • i tried with AuthorizeForScopes , but it didn't work in my case . my case was The createFolderSharePoint Method would be call inside the ActionMethod within Controller. and it seems the AuthorizeForScopes workable only inside ActionMethod .. – Hasan Othman May 07 '20 at 10:52
  • @JimXu Where does the {parent-item-id} stand for? I cannot find this as a field – Mister Verleg May 14 '20 at 15:01
  • @HasanOthman the {parent-item-id} is the path of the folder that you want to create the folder under it. It should be like `root:/{path-relative-to-root}:/` – Jim Xu May 15 '20 at 01:27
  • @JimXu if I have the folder already created and I want to use that (not delete or rename it), then what will be value for attribute "@microsoft.graph.conflictBehavior" ? Can you let me know this ? – Sagnik Chatterjee May 19 '22 at 08:28