Saturday, August 10, 2019

How to create a SharePoint Folder in C#


// SharePoint Client DLL
using Microsoft.SharePoint.Client;


public static void createDMSFolder(string rootFolder, string newFolderName, IOrganizationService service)
{
    string strUserName = "username";
    string strPassword = "password";
    string strDomain = "domain";
    string strURL = "https://dms.domain.in";

    using (ClientContext context = new ClientContext(strURL))
    {
        context.Credentials = new System.Net.NetworkCredential(strUserName, strPassword, strDomain);

        Microsoft.SharePoint.Client.List list = context.Web.Lists.GetByTitle(rootFolder);
        var folder = list.RootFolder;
        context.Load(folder);
        context.ExecuteQuery();

        ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
        newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
        newItemInfo.LeafName = newFolderName;
        ListItem newListItem = list.AddItem(newItemInfo);
        newListItem["Title"] = newFolderName;
        newListItem.Update();
        context.ExecuteQuery();

        CreateSubFoldersInDMS(list, context, newFolderName, service);
    }
}


private static void CreateSubFoldersInDMS(Microsoft.SharePoint.Client.List list, ClientContext context, string parentFolder, IOrganizationService xrmContext)
{
    var folders = list.RootFolder.Folders;
    context.Load(folders);
    context.ExecuteQuery();

    Dictionary<int, string> docList = new Dictionary<int, string>();
    docList.Add(100000000, "Copy of valid passport");
    docList.Add(100000001, "Copy of family book");
    docList.Add(100000003, "Personal Photo");
    docList.Add(100000004, "Curriculum Vitae (CV)");
    docList.Add(100000005, "Copy of last education degree");

    foreach (var folder in folders)
    {
        if (folder.Name == parentFolder)
        {
            foreach (KeyValuePair<int, string> l in docList)
            {
                var childFolder = folder.Folders.Add(l.Value.Replace("\\", "and"));
                childFolder.Update();
                context.ExecuteQuery();
            }
            break;
        }
    }
}



Refer more articles related to SharePoint and Dynamic CRM



No comments: