Newly added document listed as Draft with Published check mark
I used the sample code to create a new document in the Document Library. When it's completed, the document is listed as "Draft", but it has the green "Published" check mark.
Here's my code:
var addedDocument = App.WorkWith()
.DocumentLibrary(GetLiteratureLibrary().Id)
.CreateDocument()
.Do(d =>
d.Title =
"Test Document"
;
d.Description =
"Description for test document"
;
)
.CheckOut()
.UploadContent(documentContentStream, fileExtension)
.CheckInAndPublish()
.SaveChanges();
Hi David,
This is because the item has ContentLifeCycleStatus Live, but the WorkFLow state is not set to published (if you have an active workflow setting it directly to published would be against the workflow rules). However, if you don't use any workflow, then you can try the below sample, it will also set the ApprovalWorkflowState to Published:
var myDocument= App.WorkWith()
.DocumentLibrary(parentID)
.CreateDocument()
.Do(d =>
d.Title = documentName;
d.Description = description;
)
.CheckOut()
.UploadContent(docFile.OpenRead(), docFile.Extension)
.CheckIn()
.Do(d =>
d.ApprovalWorkflowState =
"Published"
;
).Publish()
.SaveChanges();
Thanks. That fixed the issue with creating a document.
Now I'm trying to update the content of that document, and I'm getting an error. Here's the code:
App.WorkWith()
.Document(docId)
.Do(d =>
d.Status = ContentLifecycleStatus.Master;
)
.CheckOut()
.UploadContent(documentContentStream, fileExtension)
.CheckIn()
.Do(d =>
d.ApprovalWorkflowState =
"Published"
;
)
.Publish()
.SaveChanges();
Hi David,
Here is a sample code that illustrates how to reupload an item
public
void
ReplaceDocument(Telerik.Sitefinity.Libraries.Model.MediaContent content, System.IO.Stream source)
var fluent = App.WorkWith();
//find if the document already exists by title, or introduce other business rule
var facade = fluent.Documents().
Where(d => d.Title == content.Title &&
d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master
&& d.Id != content.Id
&&
d.Parent.Id == content.Parent.Id
//check if it the replaced document in the same album as prev. document
);
int
count = 0;
facade.Count(
out
count);
if
(count > 0)
//gets first document facade
var firstDocumentFacade = facade.First();
var oldFileId = firstDocumentFacade.Get().FileId;
//change id to the new file id
firstDocumentFacade
.Do(s =>
s.FileId = content.FileId;
//update extension
s.Extension = content.Extension;
//file extension
);
var documentItem = firstDocumentFacade.Get();
//update content data
base
.UploadDataOnly(documentItem, source);
firstDocumentFacade.SaveAndContinue();
content.FileId = Guid.NewGuid();
//delete newly inserted document
base
.Delete((Document)content);
//clear chunks of old media item
this
.StorageProviderDecorator.ClearChunks(oldFileId);
else
base
.UploadDataOnly(content, source);
Hi Ivan,
Thanks for the code example. I couldn't find a good example for creating an instance of LibrariesDataProvider, so I tried this:
var libProvider = LibrariesManager.GetManager().Provider;
Hello David,
It turned out that UploadDataOnly is marked as a private method. The Upload method calls UploadDataOnly so you can try use it.
All the best,
Ivan Dimitrov
the Telerik team
I mentioned two methods. What about the other method - StorageProviderDecorator.ClearChunks ? Is there a public method that is equivalent?
Thanks,
David
Hi David,
This is no more used. It is handled by OpenAccessBlobStorageProvider which in its basis inherits from the base class for data providers.
Regards,
Ivan Dimitrov
the Telerik team
I started implementing the code example for updating the document. However, before I could get that far, I ran into a new error when adding a document.
I took my original code:
var addedDocument = App.WorkWith()
.DocumentLibrary(GetLiteratureLibrary().Id)
.CreateDocument()
.Do(d =>
d.Title =
"Test Document"
;
d.Description =
"Description for test document"
;
)
.CheckOut()
.UploadContent(documentContentStream, fileExtension)
.CheckInAndPublish()
.SaveChanges();
The fluent api to publish items programmaticly only 'pretends' to work. If i publish a document item with your suggested answer Boyan then the document item in the backend does have the checkmark icon and says it is published sure. But, the actions drop down says I can still publish the document, which I would expect to say unpublish!
This is a problem for me because while I have a control that users can / upload / download and edit document properties such as title / description. If the item isn't published correctly then non-administrators cannot download the item (navigating to the mediaurl).
I got around this by switching to the native api. But this then goes back to the problem of the Checkmark with Draft text posted originally in this thread. Im using v 4.2.1650.0
Hi Kristian,
Thank you for bringing our attention to this. I have logged a bug concerning this functionality, and we'll be working on providing a fix for it in the upcoming releases. In the meantime please accept our apologies for the inconvenience caused, we'd recommend you to use the native API sample instead.
Greetings,
Boyan Barnev
the Telerik team