Document URL property throws null reference exception
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"); 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; 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); );
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();