I am new to SharePoint, I want to know how host named site collections, path based site collections, and their files are stored in SharePoint databases. How to upload files to host named site collections programmatically?

Any reference would be appreciated where I can get this information.



I want to do it programmatically in C#. What difference will be there while uploading file to host named site collection and path based site collection? What is the role of unique URL that is specified for host named site collection?

For SharePoint databases, I found this.

  • When you say "programmatically" are you wanting to build a c# app, PowerShell script, or use the REST API? There are lots of ways to interact with SharePoint "programmatically".
    – willman
    Commented Feb 13, 2020 at 11:37
  • While the process of creating a host-named site collection is considerably different, the various API's for interacting with SharePoint programmatically against already existing site collections don't usually differ for host-named ones.
    – willman
    Commented Feb 13, 2020 at 11:40
  • @willman Thanks for your comments. Updated the question Commented Feb 13, 2020 at 11:52
  • I will also note that it is prohibited to access the Sharepoint SQL database directly. To do anything programmatically with Sharepoint, you must use one of the API'S.
    – willman
    Commented Feb 13, 2020 at 13:27
  • @willman Can you plz share which API and functions can be used for host named site collections? any example would help me a lot. I am not going to change any database, but want to know how site collections and specially host named site collection are stored in database and their files. Commented Feb 14, 2020 at 5:03

1 Answer 1


Most API calls do not differ between referencing a host-named site collection versus a "regular" site collection, since you specify the absolute url of the site to initiate a ClientContext.

If you have not used the SharePoint CSOM before, you will need to get the nuget package.

To upload a file to a document library using the c# .NET Client-Side Object Model (CSOM) API, use the following:

public void UploadDocument(string siteURL, string serverRelativeDestinationFilenamePath, string fullLocalFilenamePathToImport)
    using (ClientContext context = new ClientContext(siteURL))
        using (FileStream fs = new FileStream(fullLocalFilenamePathToImport, FileMode.OpenOrCreate))
           Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, serverRelativeDestinationFilenamePath, fs, true);
        var newFile = web.GetFileByServerRelativeUrl(serverRelativeDestinationFilenamePath);

        //check out to make sure not to create multiple versions

        var item = newFile.ListItemAllFields;

        // update some metadata if you need to, for example:
        // item["Title"] = "My Uploaded File";
        // item["CustomFieldName"] = "Some Custom Value";
        // item.Update();
        // context.ExecuteQuery();      

        // use OverwriteCheckIn type to make sure not to create multiple versions 
        newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
  • Thanks willman. In my case host named site collection URL is "http ://my-hnsc" and web application is "http ://sharepoint.testlab.pvt:80". Which URL should be used in siteURL, and serverRelativeDestinationFilenamePath? Commented Feb 18, 2020 at 10:01
  • It doesn't matter, "server-relative" means the URL starts at the slash After the server name. So, if you want to upload a file to my-hnsc/documents/myfile.docx, then the server-relative URL is "/documents/myfile.docx". If there are places in code where you need the absolute URL (like SiteUrl), you generally use the host-named URL unless you are specifically getting a WebApplication object reference.
    – willman
    Commented Feb 18, 2020 at 10:32
  • If you are using this particular sample code with a host-named site collection, the "real" web application url will not be used anywhere. When accessing a Site or anything contained in a site, the host-named site url is the "official" url of that site.
    – willman
    Commented Feb 18, 2020 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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