0

I am quite new in CSOM/Sharepoint development and have an interesting task to solve. Unfortunately I don't make as much progress with some details as I would like. A customer of mine wants a secure way to exchange confidential documents. Therefore my idea was to create a folder in Sharepoint for each customer and to share it with the customer as a guest user. So the customer has access to "his" document folder and can upload and download documents there. I can do this manually as well, but I would like to solve this programmatically via CSOM.

The programmatic creation of the folder I have managed so far. Also the file upload is no problem. However, the release of the folder of the customer is problematic. I managed to share the folder so that the customer can log in with his Microsoft account or that the order is released "for everyone".

What I need is a personal release WITHOUT forcing an MS account. An personal link with verification code etc...

This is my code so far.

public static void CreateAndShareFolder(ClientContext context, string folderName, string userEmail)
{
    var listTitle = "Dokumente";
    var rootFolder = "Testfolder_ROOT/";

    var folder = CreateFolder(context.Web, listTitle, rootFolder + folderName);

    var users = new List<UserRoleAssignment>();
    users.Add(new UserRoleAssignment()
    {
        UserId = userEmail,
        Role = Role.Edit,
    });

    var serverRelativeUrl = folder.ServerRelativeUrl;
    var absoluteUrl = new Uri(context.Url).GetLeftPart(UriPartial.Authority) + serverRelativeUrl;

    /* User gets Email, but with "public" Sharing-Link */
    //var userSharingResults = DocumentSharingManager.UpdateDocumentSharingInfo(context, absoluteUrl, users, true, true, true, null, true, true);
    /* User gets Email, but needs an MS-Account to View */
    var userSharingResults = DocumentSharingManager.UpdateDocumentSharingInfo(context, absoluteUrl, users, false, false, true, null, false, true);

    context.ExecuteQuery();
}

// From Stackoverflow: https://stackoverflow.com/a/22010815/3062062
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;
}

Your Answer

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

Browse other questions tagged or ask your own question.