dynamically create SharePoint Folder and set modified by userid?

mnirmala

New member
Joined
Jan 24, 2025
Messages
2
Programming Experience
10+
Is it possible to
using azure AD get userid for an email account using graph api and
dynamically create SharePoint Folder and set "modified by" and "created by" to azure userid
 
Last edited by a moderator:
Definitely yes if you use user delegated permissions. I have some doubts if you use app permissions.
 
Moving to "Other" under "Components and Controls" since this is a Graph API and SharePoint question rather than a C# question.

I wonder if we'll eventually need a public/commercial web API subforum for dealing with the various web APIs exposed by companies and orgs.
 
When I tried to use SharePoint Client CSOM
login using Service account, create the folder and then set authot and editor-it still shows service account as created by and modified by
C#:
 string folderUrl = site.ServerRelativeUrl +"/"+libraryName + "/" + folderName;
folder = site.Folders.Add(folderUrl);
 context.ExecuteQuery();
 context.Load(folder, f => f.ServerRelativeUrl, f => f.Name);
 context.ExecuteQuery();
 Microsoft.SharePoint.Client.ListItem listItem = folder.ListItemAllFields;
 context.Load(listItem);
 context.ExecuteQuery();
 Microsoft.SharePoint.Client.User SetToUser = site.EnsureUser("i:0#.f|membership|" + emailId);
                      
 context.Load(SetToUser);

 context.ExecuteQuery();

 listItem["Author"] = SetToUser;
 listItem["Editor"] = SetToUser;
 folder.Update();
 context.ExecuteQuery();
 
Last edited by a moderator:
CSOM does not use GraphAPI. You asked about Graph API.
 
This should get you on the right path.

How to create Folder (including nested) via CSOM in SharePoint/Modify​

See Bottom Code:​

create SharePoint Folders:
/// <summary>
/// Create Folder client object
/// </summary>
/// <param name="web"></param>
/// <param name="listTitle"></param>
/// <param name="fullFolderUrl"></param>
/// <returns></returns>
public static Folder CreateFolder(Web web, string listTitle, string fullFolderUrl)
{
    if (string.IsNullOrEmpty(fullFolderUrl))
        throw new ArgumentNullException("fullFolderUrl");
    var list = web.Lists.GetByTitle(listTitle);
    return CreateFolderInternal(web, list.RootFolder, fullFolderUrl);
}

private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderUrl)
{
    var folderUrls = fullFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
    string folderUrl = folderUrls[0];
    var curFolder = parentFolder.Folders.Add(folderUrl);
    web.Context.Load(curFolder);
    web.Context.ExecuteQuery();

    if (folderUrls.Length > 1)
    {
        var subFolderUrl = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
        return CreateFolderInternal(web, curFolder, subFolderUrl);
    }
    return curFolder;
}



Usage:

 using (var ctx = new ClientContext("https://contoso.onmicrosoft.com/"))
 {
       ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", "password");
       var folder = CreateFolder(ctx.Web, "Shared Documents", "FolderA/SubFolderA/SubSubFolderA");
 }


How to get Folder client object:

public static Folder GetFolder(Web web, string fullFolderUrl)
{
    if (string.IsNullOrEmpty(fullFolderUrl))
        throw new ArgumentNullException("fullFolderUrl");

    if (!web.IsPropertyAvailable("ServerRelativeUrl"))
    {
        web.Context.Load(web,w => w.ServerRelativeUrl);
        web.Context.ExecuteQuery();
    }
    var folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + fullFolderUrl);
    web.Context.Load(folder);
    web.Context.ExecuteQuery();
    return folder;
}

Usage:

var existingFolder = GetFolder(ctx.Web, "Shared Documents/FolderA/SubFolderA/SubSubFolderA");


Modify:

You can update list item field values

// checkout
uploadfile.CheckOut();

// get list item field values
ListItem item = uploadfile.ListItemAllFields;

// ensure User object, specify the correct login name
User anotherUser = context.Web.EnsureUser("Contoso\\JohnDoe");
context.Load(anotherUser);
context.ExecuteQuery();

// update Author and Editor field
item["Editor"] = anotherUser;
item["Author"] = anotherUser;

// update item
item.Update();

// checkin
uploadfile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);

context.ExecuteQuery();
 
@Rythorian : The sample code you presented is essentially the same the code the OP is trying. Notice both of you are using the ListItemAllFields property to try to access the backing list item for the folder and change the creator and editor.

In my experience, the newer versions of CSOM, specially when talking to SharePoint Online do not respect the attempts to spoof who created the folder.
 
Last edited:

Latest posts

Back
Top Bottom