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

Forums / Developing with Sitefinity / Questions About Document Creation

Questions About Document Creation

6 posts, 2 answered
  1. Walt
    Walt avatar
    6 posts
    Registered:
    16 Jan 2013
    26 Jul 2013
    Link to this post
    I have been working on creating documents through the
    Sitefinity API and I have downloaded the sample code, but have been having some
    issues with it.  I have modified the code to try some things.  My
    goal is to:

     Move all of the documents (mostly pdfs and docx) onto the
    server

    Programmatically create document products to a library
    without having to upload the actual documents

    Not have to stream the file when uploading through API

     After testing a few times, my code seems to always produce
    the following:

    A document under the correct library, but that has
    no icon (pdf for example) and
    cannot be accessed or downloaded and

    that is in draft mode only even when I specify Publish
    through the workflow manager
    My  Code is below:

    private void CreateDocumentNativeAPI(Guid masterDocumentId, Guid parentDocumentLibraryId, string documentTitle, string documentExtension,String DocumentN)
            {
                LibrariesManager librariesManager = LibrariesManager.GetManager();
                Document document = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
               
                if (document == null)
                {                // The document is created as master. The masterDocumentId is assigned to the master version.
                    document = librariesManager.CreateDocument(masterDocumentId);
                    // Set the parent document library.                DocumentLibrary documentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.Id == parentDocumentLibraryId).SingleOrDefault();                document.Parent = documentLibrary;
                    // Set the properties of the document.                document.Title = documentTitle;
                    document.DateCreated = DateTime.UtcNow;
                    document.PublicationDate = DateTime.UtcNow;
                    document.LastModified = DateTime.UtcNow;
                    document.UrlName = ""; // Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                    document.MimeType = "application/pdf";
                    document.FilePath = @"\\NetworkShare\filename.pdf";
                    //document.ResolveMediaUrl(false);
                    document.Visible = true;
                    document.Status = ContentLifecycleStatus.Master;
                    librariesManager.Lifecycle.Publish(document);
                    librariesManager.SaveChanges();
                    librariesManager.Lifecycle.Publish(document);
                    librariesManager.SaveChanges();
                    var bag = new Dictionary<string, string>();
                    bag.Add("ContentType", typeof(Document).FullName);                WorkflowManager.MessageWorkflow(masterDocumentId, typeof(Document), null, "Publish",false, bag);
                }        }
    Answered
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    31 Jul 2013 in reply to Walt
    Link to this post
    I'm not sure if things have changed in recent versions of Sitefinity, but I don't believe you can specify the filepath location of the file directly using the FilePath property. 

    Instead, you need to actually upload the document to Sitefinity and let it set the FilePath itself based on where it places it and names it (assuming you're using the File System storage).

    To do this, you need to use the method, using a stream to the file for upload:

    librariesManager.Upload(document, documentStream, documentExtension);

    as shown on this documentation page: http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/media-modules/documents-and-files/managing-documents/creating-documents

    I hope this is helpful, let me know if I've misunderstood your question!
  3. Walt
    Walt avatar
    6 posts
    Registered:
    16 Jan 2013
    01 Aug 2013
    Link to this post

    I modified the code a bit so that I was actually uploading the file through a stream.  My code is:

    private void TempMigrateDocumentNativeAPI(Guid masterDocumentId, Guid parentDocumentLibraryId,
                  string documentTitle, string documentExtension, String DocumentN,
                  String argMimeType, String FilePath, String URLName)
            {
                LibrariesManager librariesManager = LibrariesManager.GetManager();
                Document document = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
     
                using (StreamReader sr = new StreamReader(FilePath))
                {
     
                    Stream stream = sr.BaseStream;
     
                    if (document == null)
                    {
                         
                        document = librariesManager.CreateDocument(masterDocumentId);
                        DocumentLibrary documentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.Id == parentDocumentLibraryId).SingleOrDefault();
                        document.Parent = documentLibrary;
                        document.Title = documentTitle;
                        document.DateCreated = DateTime.UtcNow;
                        document.PublicationDate = DateTime.UtcNow;
                        document.LastModified = DateTime.UtcNow;
                        document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                        document.Visible = true;
                        document.Status = ContentLifecycleStatus.Master;
                        librariesManager.Upload(document, stream, documentExtension);
                        librariesManager.Provider.RecompileItemUrls(document);
                        librariesManager.SaveChanges();
                        document.ApprovalWorkflowState = "Published";
                        librariesManager.Lifecycle.Publish(document);
                        librariesManager.SaveChanges();
                        //...
                    }
     
                }
            }

    I also removed the line of code where I set the file path, since I am uploading via a stream, however I am still not able to open the file via the open file link in the Sitefinity Interface.  When I view all files that I have uploaded via this script, they all look published, however the problem is that there is no valid link. 

    An example of what I see in the user interface is:
    Published document
    I click on published document to see the detail, then I click on Open the File and receive this error:

    Value cannot be null.
    Parameter name: item
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
     Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: item
     
    Source Error:
     
     
     An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
     
    Stack Trace:
     
     
     
    [ArgumentNullException: Value cannot be null.
    Parameter name: item]
       Telerik.Sitefinity.Lifecycle.LifecycleDecorator.GetTemp(ILifecycleDataItemGeneric item, CultureInfo culture) +954
       Telerik.Sitefinity.Lifecycle.LifecycleDecorator.Telerik.Sitefinity.Lifecycle.ILifecycleDecorator.GetTemp(ILifecycleDataItem cnt, CultureInfo culture) +38
       Telerik.Sitefinity.Modules.Libraries.Web.LibraryHttpHandler.GetContentItem(RouteValueDictionary dataTokens, String& redirectUrl, LibrariesManager& manager) +415
       Telerik.Sitefinity.Modules.Libraries.Web.LibraryHttpHandler.GetOutputItem(HttpRequest request, HttpResponse response, ICacheManager cache, String key, MediaContent& content) +99
       Telerik.Sitefinity.Modules.Libraries.Web.LibraryHttpHandler.ProcessRequest(HttpContext context) +731
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +625
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270

    The original content Id is: 00000000-0000-0000-0000-000000000000 for all files that are uploaded via the code. above.





  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    01 Aug 2013 in reply to Walt
    Link to this post
    could it be that you are using the Lifecycle.Publish instead of the worfklow as shown in the example:

    //Publish the DocumentLibraries item. The live version acquires new ID.
    var bag = new Dictionary<string, string>();
    bag.Add("ContentType", typeof(Document).FullName);
    WorkflowManager.MessageWorkflow(masterDocumentId, typeof(Document), null, "Publish", false, bag);

    See if using the workflowmanager instead gives you the result you expect. if not, I'd suggest posting the code to a support ticket as it all looks correct to me.

    I hope this was helpful!
  5. Walt
    Walt avatar
    6 posts
    Registered:
    16 Jan 2013
    01 Aug 2013 in reply to SelAromDotNet
    Link to this post
    WorkflowManager hadn't initially been working when I run in localhost mode and wasn't sure how to resolve that, but the problem ended up being a logistical file issue storage problem.  Sitefinity stores the files under App_Data\Storage\FileSystem.  When I was running on localhost using a remote database, the files that I was streaming to upload ended up on my local drive, and when I checked the other hosted site that was using the same database I was getting the link errors.  I started to check on local host, then I am either thinking to upload the files to the remote App_Data\Storage\FileSystem, or just run the code from the remote host from the get-go.
    Answered
  6. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    01 Aug 2013 in reply to Walt
    Link to this post
    ah yes if you are using the file system, the contents to get stored locally. Simply copying the contents of the App_data/storage folder to the remote site should resolve the issue, as it looks by ID to find the item in that folder, which is what is used to name each file.

    Let me know if that doesn't work for you
6 posts, 2 answered