0

I created a document library by code, and I need to return the URL of the All Items page.

I tried this, but its not what I am looking for

 SPList oSPList = oSPWeb.Lists.TryGetList(DocumentLibraryName);
                        if (oSPList == null)
                        {
                            Guid ID = oSPWeb.Lists.Add(DocumentLibraryName, DocumentLibraryName + System.DateTime.Now.ToString(), SPListTemplateType.DocumentLibrary);
                            oSPList = oSPWeb.Lists[ID];
                            DocumentLibraryLink = oSPList.DefaultViewUrl;
3

I'm guessing that you want the complete url to the Default view and not just the server relative url to the view. If that is true, you can do the following to get the easily get the full url.

DocumentLibraryLink = SPUtility.ConcatUrls(oSPWeb.Url, oSPList.DefaultView.Url);

The result is something like this

http://servername/sitecollection/site/lists/listname/defaultpagename.aspx

If the view is not the default view, you can use this. ViewName is the Display Name for the view.

DocumentLibraryLink = SPUtility.ConcatUrls(oSPWeb.Url, oSPList.Views[ViewName].Url);

This will give the url even if it is located in the FORMS folder.

http://servername/sitecollection/site/lists/listname/forms/viewname.aspx
5
  • efaultviewurl is dispform.aspx, I need the link to /Forms/Allitems.aspx. but I dont want to hardcode i Mar 21 '12 at 12:26
  • so is allitems the default view for this list?
    – Rich Ross
    Mar 21 '12 at 12:32
  • yes its always the default view. Mar 21 '12 at 12:44
  • this should work? DocumentLibraryLink = SPUtility.ConcatUrls(oSPWeb.Url, oSPList.Views["All Documents"].Url); Mar 21 '12 at 12:46
  • That worked for me in quick mock test on my development instance using an OOB document library. Also, if it is always the default view then DefaultView.Url returns the correct url in SP2010. this is a different property than DefaultViewUrl which returns the server relative path.
    – Rich Ross
    Mar 21 '12 at 12:47
0

Try

SPUrlUtility.CombineUrl(oSPWeb.Site.Url,oSPList.DefaultViewUrl);
2
  • defaultviewurl is dispform.aspx, I need the link to /Forms/Allitems.aspx. but I dont want to hardcode i. Mar 21 '12 at 12:22
  • That is strange I get /Forms/Allitems.aspx using that code Mar 21 '12 at 13:41

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.