Document Upload Problem
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();
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
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();
doc.Id = Guid.NewGuid();
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);