1

I am new to this site so please feel free to redirect me to other posts that may answer my two part question.

I am working on a .NET web form that needs to upload files from a asp:FileUpload control , directly to a SP Online Document Library.

I have an Office 365 username and password to authenticate with.

I need to programatically create a 3 level sub-folder in a Document Library.

Example: Contact/John Doe/entityname/title_guid where title_guid is the new folder to be created and Contact is the Library Name.

After the folder is created, I need to upload the file selected in the asp:FileUpload control. I already know how to get to the file.FileBytes of the selected file, just not sure how to send those to the document library.

I understand that there are some .NET libraries I can use, but there is also a REST endpoint?
I could use either method, whatever I can find that has good code examples I can interpret and modify for my needs.

0

To get you started, here is a way how to create a folder in a DocumentLibrary:

    internal static void CreatePhysicalFolderStructure(ClientContext context)
    {
        //get list of all folders from the zip file
        var allFolders = Globals.FolderStructure.Descendants().Where(n => n.FolderLevel > 1).ToList();

        //get reference to the list
        List list = context.Web.Lists.GetByTitle(Globals.DocLibName);

        CreateFolder(list, "Excel Files");
        CreateFolder(list, "Excel Files/2013");
        CreateFolder(list, "Excel Files/2013/PDF");

        'executes query
        context.ExecuteQuery();
    }
    internal static void CreateFolder(List list, string name)
    {
        var info = new ListItemCreationInformation
        {
            UnderlyingObjectType = FileSystemObjectType.Folder,
            LeafName = name
        };
        var newItem = list.AddItem(info);
        newItem["Title"] = name;
        newItem.Update();
    }

Please note that you have to create folder in order. If you would try to create directly the very last folder Excel Files/2013/PDF", the method would fail.

Also beware that you cannot create already existing folder. Here is the method, which will return list of all folders that are already existing (you will have to modify it a bit).

private static IEnumerable<string> GetListFolderStructure(ClientContext context, List list)
    {
        var results = new List<string>();

        //query to recursively loop through all folders
        var qry = new CamlQuery();
        qry.ViewXml = "<View Scope='RecursiveAll'>" +
                          "<Query>" +
                              "<Where>" +
                                    "<Eq>" +
                                         "<FieldRef Name='FSObjType' />" +
                                         "<Value Type='Integer'>1</Value>" +
                                    "</Eq>" +
                             "</Where>" +
                           "</Query>" +
                        "</View>";

        var allItems = list.GetItems(qry);
        context.Load(allItems, n => n.Include(x => x.DisplayName, x => x.Folder));
        context.ExecuteQuery();

        //retrieve default path that we will trim from our URL result
        //e.g. Result we get from query is:     /sites/RobertTest/MainFolder/TestFolder/Word Files
        //but we want to only return:           TestFolder/Word Files
        string sDefaultPath = new Uri(Globals.SiteURL).AbsolutePath + "/" + Globals.DocLibName + "/";

        //add results to our List
        foreach (var item in allItems)
        {
            results.Add(item.Folder.ServerRelativeUrl.Replace(sDefaultPath, ""));
        }

        return results;
    }

Hope this would help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.