using API, Document not being found after being published in the backend
I have a custom module that I am trying to attach a document to in the backend. Using the samples provided in the documentation I first create a document... and save the document.ID in a guid docId.
Guid docId = new Guid();
//The document is created as master. The masterDocumentId is assigned to the master version.
App.WorkWith().DocumentLibrary(parentLibrary.Id).CreateDocument().CheckOut()
.Do(document =>
//Set the properties.
docId = document.Id;
document.Title = documentTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.Description = description;
document.Urls.Clear();
document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
)
.UploadContent(fileStream, documentExtension)
//Save the changes.
.SaveChanges();
//Publish the document. The live version acquires new ID.
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(Document).FullName);
WorkflowManager.MessageWorkflow(docId, typeof(Document), null, "Publish", false, bag);
return docId;
After publishing the document, I return that docId GUID field the function that creates the custom module and save it as documentId... in that logic...I have the following code
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type cdeapportionmentsType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.CDEApportionments.cdeapportionments");
DynamicContent cdeapportionmentsItem = new DynamicContent();
cdeapportionmentsItem = dynamicModuleManager.CreateDataItem(cdeapportionmentsType);
// This is how values for the properties are set
cdeapportionmentsItem.SetValue("Owner", SecurityManager.CurrentUserId);
cdeapportionmentsItem.SetValue("Desc", description);
cdeapportionmentsItem.SetValue("PublicationDate", DateTime.Now);
cdeapportionmentsItem.SetValue("res", res);
cdeapportionmentsItem.SetValue("FiscalYear", fiscalYear);
cdeapportionmentsItem.SetValue("cdeDate", cdeDate);
cdeapportionmentsItem.SetValue("ClaimNumber", claimNR);
LibrariesManager libraryManager = LibrariesManager.GetManager();
Document document = libraryManager.GetDocument(documentId);
if (document != null)
cdeapportionmentsItem.AddFile("Attachment", document.Id);
BUT...I am getting itemnotfoundexception at the following line...
Document document = libraryManager.GetDocument(documentId);
as an example, I have stepped through the code and the GUID that I am returning into docID and consequently the documentId fields would be something like
8F66F057-EA17-453A-861B-2741BAC81363, yet when I look in the database the id fields for the master and live version of the file would be
CF1D2260-3361-4110-AE55-31424CD1D6A3 and
66FDEA24-5D9B-4B94-82A8-FD9DA58FEEB2....
am I missing something???
I've had some success with the following method. I used this to update some items that had been previously created:
LibrariesManager libraryManager = LibrariesManager.GetManager();
Document document =
null
;
DynamicContent customItem = dynamicModuleManager.GetDataItems(customType).Where(i => i.GetValue<
string
>(
"Title"
) == title).FirstOrDefault();
DynamicContent master = dynamicModuleManager.Lifecycle.Edit(customItem)
as
DynamicContent;
DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master)
as
DynamicContent;
document = libraryManager.GetDocuments().FirstOrDefault(i => i.Title == fileName && i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
if
(document !=
null
)
temp.AddFile(
"Attachments"
, document.Id);
master = dynamicModuleManager.Lifecycle.CheckIn(temp)
as
DynamicContent;
dynamicModuleManager.Lifecycle.Publish(master);
dynamicModuleManager.SaveChanges();
since I can have files with the same title...not sure if that query would work out for me...instead...I changed my CreateDocument logic from the fluent api to the native api and created a document with a "predefined ID". That GUID stuck throughout my logic and I was able to attach it to the custom module with no problem. Makes me curious if this is some sort of a bug...
I was able to get past my issue of not being able to "attach" a document to a custom module in the codebehind using the api...but now I am experiencing another issue. When I login to the backend and create a custom module and upload a file, everything works fine. But when I use the following code to create a custom module using the API....it behaves strangely.
Guid ownerId = SecurityManager.CurrentUserId;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type cdeapportionmentsType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.CDEApportionments.cdeapportionments");
DynamicContent cdeapportionmentsItem = new DynamicContent();
cdeapportionmentsItem = dynamicModuleManager.CreateDataItem(cdeapportionmentsType);
// This is how values for the properties are set
cdeapportionmentsItem.SetValue("Owner", ownerId);
cdeapportionmentsItem.SetValue("Desc", description);
cdeapportionmentsItem.SetValue("PublicationDate", DateTime.Now);
cdeapportionmentsItem.SetValue("res", res);
cdeapportionmentsItem.SetValue("FiscalYear", fiscalYear);
cdeapportionmentsItem.SetValue("cdeDate", cdeDate);
cdeapportionmentsItem.SetValue("ClaimNumber", claimNR);
Lstring url = Regex.Replace(description.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
cdeapportionmentsItem.SetValue("UrlName", url);
cdeapportionmentsItem.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master;
cdeapportionmentsItem.Visible = true;
outCdeGuid = cdeapportionmentsItem.Id;
LibrariesManager libraryManager = LibrariesManager.GetManager();
Document document = libraryManager.GetDocument(documentId);
if (document != null)
cdeapportionmentsItem.AddFile("Attachment", document);
// We can now call the following to publish the item
dynamicModuleManager.Lifecycle.Publish(cdeapportionmentsItem);
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
var bag = new Dictionary<string, string>();
bag.Add("ContentType", "Telerik.Sitefinity.DynamicTypes.Model.CDEApportionments.cdeapportionments");
WorkflowManager.MessageWorkflow(outCdeGuid, cdeapportionmentsType, null, "Publish", false, bag);
Everything works fine in the codebehind. But when I go to my "live site" the widget showing my custom module doesnt have a file link attached to it. and when I login to the backend, and check the panel editor for the custom module, the document is listed there? I attached two pics showing this...
any help would be great, thanks!
Hello Dustin,
That's indeed interesting - the AddFile extension method should create the proper ContentLink and set the document correctly for the newly created item (you can actually use the AddFile(string propertyName, Guid fileId) overload and pass in directly the document ID if you already have it).
We have tested the scenario on a local project running the same Sitefinity version, and the item has been properly created with the document appearing both on the frontend and the backend.
Just to make sure we're following the official module documentation reference you can try doing the Publishing through Lifecycle instead through the Workflow, for example:
public
void
CreateWvwv()
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var providerName = String.Empty;
// Set the culture name for the multilingual fields
var cultureName =
"en"
;
Thread.CurrentThread.CurrentUICulture =
new
CultureInfo(cultureName);
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type wvwvType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.vwev.Wvwv"
);
DynamicContent wvwvItem = dynamicModuleManager.CreateDataItem(wvwvType);
// This is how values for the properties are set
wvwvItem.SetString(
"Title"
,
"Some Title1"
, cultureName);
wvwvItem.SetString(
"UrlName"
,
"SomeUrlName1"
, cultureName);
// Set the selected value
wvwvItem.SetValue(
"ChoiceTest"
,
"Option2"
);
wvwvItem.SetString(
"wvv"
,
"Some wvv"
, cultureName);
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
var Tag = taxonomyManager.GetTaxa<FlatTaxon>().Where(t => t.Taxonomy.Name ==
"Tags"
).FirstOrDefault();
if
(Tag !=
null
)
wvwvItem.Organizer.AddTaxa(
"Tags"
, Tag.Id);
wvwvItem.SetValue(
"Owner"
, SecurityManager.GetCurrentUserId());
LibrariesManager libraryManager = LibrariesManager.GetManager();
var image = libraryManager.GetImages().FirstOrDefault(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
if
(image !=
null
)
wvwvItem.AddImage(
"Images"
, image.Id);
wvwvItem.SetValue(
"PublicationDate"
, DateTime.Now);
wvwvItem.SetString(
"ShortField"
,
"Some ShortField"
, cultureName);
var document = libraryManager.GetDocuments().FirstOrDefault(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
if
(document !=
null
)
wvwvItem.AddFile(
"Documents"
, document);
wvwvItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName,
"Draft"
,
new
CultureInfo(cultureName));
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
// We can now call the following to publish the item
ILifecycleDataItem publishedWvwvItem = dynamicModuleManager.Lifecycle.Publish(wvwvItem);
//You need to set appropriate workflow status
wvwvItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName,
"Published"
);
dynamicModuleManager.SaveChanges();
after looking at your posts...I realized I wasn't grabbing the "live" version of the Document...I changed my logic to
Document document = libraryManager.GetDocument(documentId);
if (document != null)
document = libraryManager.Lifecycle.GetLive(document) as Document;
cdeapportionmentsItem.AddFile("Attachment", document);
and it is working great now. Thanks!