1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Document Library fluent API 4.4 issue

Document Library fluent API 4.4 issue

3 posts, 1 answered
  1. readonlychild
    readonlychild avatar
    67 posts
    Registered:
    26 May 2011
    27 Jan 2012
    Link to this post
    Hi Everyone,

    I have this method working fine in version 4.1 SP3,
    but will throw the exception below when in version 4.4

            public static string GetDocumentUrl(string library, string docName)
            {
                string r = string.Empty;
                 
                var lib = App.WorkWith().DocumentLibraries().Where(l => l.Title == library).Get().FirstOrDefault();
                if (lib != null)
                {
                    var doc = lib.Documents.Where(d => d.Title == docName).FirstOrDefault();
                    if (doc != null)
                    {
                        r = doc.MediaUrl;
                    }
                }
                return r;
            }

    exception is :

    System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Telerik.Sitefinity.Modules.Libraries.MediaContentExtensions.ResolveMediaUrl(MediaContent mediaContent, Boolean resolveAsAbsoluteUrl)
       at Telerik.Sitefinity.Abstractions.AppSettings.GetMediaItemUrl(MediaContent media)
       at Telerik.Sitefinity.Libraries.Model.MediaContent.get_MediaUrl()

    Any insight would be appreciated.

    EDIT: the method returns an empty string fine when the document does not exist in the Library, but the exception if the document exists.
  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    30 Jan 2012
    Link to this post
    Hello Ernesto,

    I have taken your code and modified it a bit, since it was returning an object reference error. Please check the following code sample as well as the changes I have made to your code:

    protected void Page_Load(object sender, EventArgs e)
           {
               GetDocumentUrl("text", "Default Library");
           }
     
           public static string GetDocumentUrl(string docName, string libName)
           {
               string r = string.Empty;
     
               LibrariesManager docManager = LibrariesManager.GetManager();
               DocumentLibrary library = docManager.GetDocumentLibraries().Where(l => l.Title == libName).FirstOrDefault();
               if (library != null)
               {
                   var doc = docManager.GetDocuments().Where(d => d.Title == docName).FirstOrDefault();
                   if (doc != null)
                   {
                       //Will return the resolved absolute URL
                       var docUrl = doc.Url;
                       //Will return the default relative URL (i.e. /documentsroot/library/document
                       var docUrl1 = doc.Urls.Where(u => u.RedirectToDefault == false).SingleOrDefault().Url;
                       //Will return the properly resolved document mediaUrl. You can use doc.ResolveMediaUrl() and pass a boolean parameter to it
                       //that will tell it to rerutn absolute or relative Url. By default it returns resolved absolute URL
     
                       var docUrl2 = doc.MediaUrl;
                       r = docUrl2;
                   }
               }
               return r;
           }

    I have added couple of new variables, when getting the document. For example docUrl1 returns the relative url to the item where docUrl = docUrl2.

    Greetings,
    Victor Velev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. readonlychild
    readonlychild avatar
    67 posts
    Registered:
    26 May 2011
    30 Jan 2012
    Link to this post
    Thanks Victor, using the LibrariesManager did the trick.
3 posts, 1 answered