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

Forums / Developing with Sitefinity / Document Upload Problem

Document Upload Problem

4 posts, 0 answered
  1. Mohamed
    Mohamed avatar
    15 posts
    Registered:
    24 Oct 2010
    11 Jul 2011
    Link to this post
    Hello,

    I have a problem with uploading a document using sitefinity apis.

    I use the code below, and when I use the debugger the code crashes at the line 
                librariesManager.Publish(doc);
    giving the following error:
    "Cannot publish an item that is not a master."

    LibrariesManager librariesManager = new LibrariesManager();
                 
                Guid newLibId = Guid.Empty;
                int libDocsCount = 0;
                App.WorkWith().DocumentLibraries().Where(i => i.Title == "CVs").Count(out libDocsCount);
                Guid docLibID = new Guid();
                if (libDocsCount == 0)
                {
                    App.WorkWith().DocumentLibrary().CreateNew().Do(dl =>
                    {
                        dl.Title = "CVs";
                        docLibID = dl.Id;
     
                    }).SaveChanges();
                }
                DocumentLibrary cvsDocLib = librariesManager.GetDocumentLibraries().Where(i => i.Title == "CVs").FirstOrDefault();
                Guid docID = new Guid();
         
                Document doc = librariesManager.CreateDocument();
                doc.Library = cvsDocLib;
                doc.Parent = cvsDocLib;
                 
                doc.ContentState = radUpLoad_CV.UploadedFiles[0].ContentType;
                doc.Extension = radUpLoad_CV.UploadedFiles[0].GetExtension();
                doc.Title = radUpLoad_CV.UploadedFiles[0].GetNameWithoutExtension();
                doc.DateCreated = DateTime.Now;
                doc.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live;
                 
                librariesManager.Upload(doc, radUpLoad_CV.UploadedFiles[0].InputStream, radUpLoad_CV.UploadedFiles[0].GetExtension());
     
                //gnerate an URL for the content item
                librariesManager.RecompileItemUrls<Document>(doc);
                 
                //up to now, the item is in Draft State. We hae to call Publish method to publish it.
                 
                librariesManager.Publish(doc);
                librariesManager.SaveChanges();
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    12 Jul 2011
    Link to this post
    Hello Mohamed,

    Pass the master state of the item when you make a publish. ContentManager has a method GetMaster that you can use to get an item in master state.

    manager.Publish(manager.GetMaster(item))

    Publish  method - publishes the content in master state. Content becomes live after the publish.

    Kind regards,
    Ivan Dimitrov
    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
  3. Mohamed
    Mohamed avatar
    15 posts
    Registered:
    24 Oct 2010
    13 Jul 2011
    Link to this post
    Thanks a lot for your reply.

    I updated my code, used the code block below and it gave my another error:
    "Id cannot be Empty Guid"
    Document doc = librariesManager.CreateDocument();
                doc.Library = cvsDocLib;
                doc.Parent = cvsDocLib;
                 
                doc.ContentState = radUpLoad_CV.UploadedFiles[0].ContentType;
                doc.Extension = radUpLoad_CV.UploadedFiles[0].GetExtension();
                doc.Title = radUpLoad_CV.UploadedFiles[0].GetNameWithoutExtension();
                doc.DateCreated = DateTime.Now;
                doc.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live;
                 
                librariesManager.Upload(doc, radUpLoad_CV.UploadedFiles[0].InputStream, radUpLoad_CV.UploadedFiles[0].GetExtension());
                 
                //gnerate an URL for the content item
                librariesManager.RecompileItemUrls<Document>(doc);
                 
                //up to now, the item is in Draft State. We hae to call Publish method to publish it.
                ContentManager contentManager = new ContentManager();
                 
                //contentManager.Publish(contentManager.GetMaster(doc));
                //contentManager.SaveChanges();
                 
                librariesManager.Publish(librariesManager.GetMaster(doc));
                librariesManager.SaveChanges();



    And when I tried to give ID to the document before publish, it gave my the following error:
    "Unsupported Operation: Change of identity is not supported."


    doc.Id = Guid.NewGuid();
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    13 Jul 2011
    Link to this post
    Hello Mohamed,

    Below is a sample code that shows how to create and upload media item

    ContentItem mediaContentItem = (ContentItem)this.manager.CreateItem(typeof(ContentItem));
    mediaContentItem.Parent = this.currentLibraryAlbum;
    mediaContentItem.Title = contentItem.Title;
    mediaContentItem.UrlName = urlName;
    mediaContentItem.Description = contentItem.Description;
    mediaContentItem.DateCreated = contentItem.DateCreated;
    mediaContentItem.PublicationDate = contentItem.PublicationDate;
    mediaContentItem.ExpirationDate = contentItem.ExpirationDate;
     
    string extension;
     
    this.manager.Upload(mediaContentItem, this.GetNextMediaContentItem(out extension), extension);
     
    this.manager.RecompileItemUrls<ContentItem>(mediaContentItem);
    this.manager.Publish(mediaContentItem);


    Greetings,
    Ivan Dimitrov
    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
4 posts, 0 answered