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

Forums / Developing with Sitefinity / Document URL property throws null reference exception

Document URL property throws null reference exception

2 posts, 0 answered
  1. Bobby Ross
    Bobby Ross avatar
    14 posts
    Registered:
    02 Nov 2004
    22 Oct 2013
    Link to this post
    I am attempting to write custom javascript controls for document management, but have hit a roadblock from the start.
    I have a controller that returns json results to an ajax call.  When attempting to access the url property to feed into my model the code throws a null reference exception.  I also attempted using the fluent API to get the documents and get null reference exceptions in the same place.
    Not sure what I am doing wrong?
    Controller Code
    public class SFDocumentController : Controller
        {
    [HttpPost]
            public ActionResult GetDocumentsByLibrary(Guid documentLibraryId)
            {
                LibrariesManager docManager = LibrariesManager.GetManager();
                DocumentLibrary library = docManager.GetDocumentLibraries().Where(l => l.Id == documentLibraryId).FirstOrDefault();
                if (library != null)
                {
                    var documentList = docManager.GetDocuments()
                        .Where(d => d.Parent.Id == documentLibraryId && d.Status == ContentLifecycleStatus.Live)
                        .Select(d => new SFDocumentView(d)).ToList();
                    return Json(documentList);
                }
                else
                {
                    return Json("Library Not Found");
                }
                 
            }
    }

    Model
    public class SFDocumentView
            {
                public SFDocumentView() { }
                public SFDocumentView(Document document)
                {
                    this.Id = document.Id;
                    this.Title = document.Title;
                    this.Description = document.Description;
                    this.Url = document.Url;
                     
                    this.TotalSize = document.TotalSize;
                }
                public Guid Id { get; set; }
                public string Title { get; set; }
                public string Description { get; set; }
                public string Url { get; set; }
                public long TotalSize { get; set; }
            }


    JS
    var libraryId = "Some Guid";
    $.ajax({
                    url: application_path + 'json/SFDocument/GetDocumentsByLibrary',
                    cache: false,
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({ documentLibraryId: libraryId }),
                    success: function (result) {
                      console.log(result);
                    }
              });

  2. Ivan A. Petrov
    Ivan A. Petrov avatar
    8 posts
    Registered:
    09 Sep 2013
    24 Oct 2013
    Link to this post
    Hi Bobby,

    Thank you for contacting Sitefinity Support!

    Try replacing this:

    var documentList = docManager.GetDocuments().Where(d => d.Parent.Id == documentLibraryId && d.Status == ContentLifecycleStatus.Live).Select(d => new SFDocumentView(d)).ToList();

    with this:

    var documentList = docManager.GetDocuments().Where(d => d.Parent.Id == documentLibraryId && d.Status == ContentLifecycleStatus.Live).ToList().Select(d => new SFDocumentView(d)).ToList();

    Please do not hesitate to contact us if you have any other problems or concerns.
    Regards,
    Ivan A. Petrov
    Telerik
    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
2 posts, 0 answered