Document URL property throws null reference exception

Posted by Community Admin on 04-Aug-2018 20:42

Document URL property throws null reference exception

All Replies

Posted by Community Admin on 22-Oct-2013 00:00

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);
                
          );

Posted by Community Admin on 24-Oct-2013 00:00

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

This thread is closed