0

So I'm trying to get the location of a document library, but I've become stuck on how to proceed. This is what I've got so far:

    public string GetLibraryLocation()
    {
        var location = "";
        SPSite siteCollection = SPContext.Current.Site;
        var siteString = siteCollection.ToString();
        //SPWebCollection collectionWebsites = siteCollection.AllWebs;

        using (SPSite site = new SPSite(siteString))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPListCollection docLibCollection = web.GetListsOfType(SPBaseType.DocumentLibrary);

                // Where to go from here..

            }
        }

        return location;
    }

What I'm trying to achieve is writing a method that can give me the location of the document library in the shape of an URL. With that URL, the plan is to obtain a folder residing in the library, which I'm then going to insert a file into. But I'm currently stuck on just obtaining the library URL. Any hints appreciated!

2
  • is it a list or document library. also why do you want to loop through all the document libraries to get one?
    – Ali Jafer
    Aug 18 '16 at 12:25
  • Good question, I probably don't want to do that actually. And it's a document library.
    – Khaine775
    Aug 18 '16 at 12:34
1

You can get the absolute URL of the document library as

web.Url + "/" + docLib.RootFolder.Url

that is the same as:

string.Concat(web.Url, "/", docLib.RootFolder.Url)

or an alternative solution:

site.MakeFullUrl(docLib.RootFolder.Url)

In your case it would be (if you would lie to iterate over all of the doc. libraries):

SPListCollection docLibCollection = web.GetListsOfType(SPBaseType.DocumentLibrary);
foreach (SPList docLib in docLibCollection)
{
  var docLibUrl = string.Concat(web.Url, "/", docLib.RootFolder.Url);
  // or
  // var docLibUrl = site.MakeFullUrl(docLib.RootFolder.Url);
}

If you know the name of the doc. library:

SPList docLib = web.Lists[docLibName];
var docLibUrl = string.Concat(web.Url, "/", docLib.RootFolder.Url);
// or
// var docLibUrl = site.MakeFullUrl(docLib.RootFolder.Url);

See this thread for reference.

BTW, if your code runs in SharePoint context (SPContext.Current is not null), I don't see, why you create new SPSite and SPWeb objects instead of using the ones available via the context.

1
  • Thanks for your reply. In this context, how do I get to my document library, the one you refer to as yourDocLib? And you're right about the SPContext, I've changed that.
    – Khaine775
    Aug 18 '16 at 13:10
0

this should help you out. No need to cycle through all document library lists in the web objects! if you know which one to pass then that's what you need.

    public void createLibraryLocation(string documentLibraryName, string fileToUpload)
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                if (!System.IO.File.Exists(fileToUpload))
                    throw new FileNotFoundException("File not found.", fileToUpload);

                SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                // Prepare to upload
                Boolean replaceExistingFiles = true;
                String fileName = System.IO.Path.GetFileName(fileToUpload);
                FileStream fileStream = File.OpenRead(fileToUpload);

                // Upload document
                SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                // Commit 
                myLibrary.Update();
            }
        }
    }

code was taken from here but amended slightly so you can call it passing two variables:

https://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

to use the method above just do the following:

createLibraryLocation("Documents", @"C:\mydocument.txt");

"documents" is the name of the document library and "mydocument.txt" is the file name located at "c" drive.

hope it helps.

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.