Setting multiple URL's on a document programmatically when uploading
Hi Guys
I have the following scenario (using Sitefinity 8);
Each month our client uploads the latest version of documents (named Jan, Feb, March, etc..) The documents are grouped in libraries (folders).
3rd parties and other systems will need to download these documents - but only the latest version.
The plan was to use the multiple URL's properties and when the user clicks the upload button do the following:
1. clear out the multiple url's for the other documents in this library (the doc's parent library)
2. set the multiple url on the doc being published to the library path (e.g. /docs/policies/individual or /docs/policies/company)
I am at the point where the code seems to work and the properties are being set but when trying to use the URL I just get a 404 error
I am using the ExecuteOnPublish event and my code is as follows:
protected
override
ILifecycleDataItemGeneric ExecuteOnPublish(ILifecycleDataItemGeneric masterItem,
ILifecycleDataItemGeneric liveItem, System.Globalization.CultureInfo culture =
null
, DateTime? publicationDate =
null
)
// see here for how to modify documents in code:
if
(masterItem
is
Document)
var documentItem = masterItem
as
Document;
// only with with documents in the factsheet archive library
if
(documentItem.Library.Title.Equals(
"FactsheetsArchive"
))
ClearMultipleUrlsOnPreviouslyUploadedDocs(documentItem);
SetDefaultMultipleUrlForUploadedDoc(documentItem);
// do the normal publishing stuff
return
base
.ExecuteOnPublish(documentItem
as
ILifecycleDataItemGeneric, liveItem, culture, publicationDate);
// do the normal publishing stuff
return
base
.ExecuteOnPublish(masterItem, liveItem, culture, publicationDate);
// build up a url that is made up of the document's file path but without the filename and extension
private
static
void
SetDefaultMultipleUrlForUploadedDoc(Document documentItem)
documentItem.Urls.Clear();
va
r docNameWithExt = documentItem.Title.Value.ToLower() + documentItem.Extension.ToLower();
var slashIdx = documentItem.FilePath.LastIndexOf(
"/"
, StringComparison.CurrentCulture);
var defaultUrl = "/" + documentItem.FilePath.Substring(0, slashIdx);
var mediaUrlData =
new
MediaUrlData();
mediaUrlData.Url = defaultUrl;
mediaUrlData.IsDefault =
false
;
documentItem.Urls.Add(mediaUrlData);
private
static
void
ClearMultipleUrlsOnPreviouslyUploadedDocs(Document documentItem)
var librariesManager = LibrariesManager.GetManager();
// Libraries are the topmost level, the hierarchy below that are referred to as folders
var folder = librariesManager.FindFolderById((Guid)documentItem.FolderId);
// get all docs we need to fiddle
var docsInFolder = librariesManager.GetDocuments()
.Where(d => d.Status == ContentLifecycleStatus.Master
&& d.FolderId == folder.Id
// only docs in this documentItem's parent folder
&& d.Id != documentItem.Id)
// exclude the doc that is being published
.OrderBy(d => d.PublicationDate);
// order by most recently published last
// - set all docs in this folder with multiple URL's to blank
// - because the docs are ordered by publication date, we should process this list
// from oldest published to newest published
foreach
(var docInFolder
in
docsInFolder)
// get the previously published document so that we can fiddle the multiple urls
var doc = librariesManager.GetDocument(docInFolder.Id);
// Check out the master to get a temp version
var tempDoc = librariesManager.Lifecycle.CheckOut(doc)
as
Document;
if
(tempDoc !=
null
)
// only change the urls if something has been captured before,
// otherwise might be updating a doc that doesn't need to be
if
(tempDoc.Urls.Count > 0)
tempDoc.Urls.Clear();
librariesManager.RecompileAndValidateUrls(tempDoc);
librariesManager.Lifecycle.CheckIn(tempDoc);
librariesManager.SaveChanges();
Any suggestions?