Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static DynamicContent CreateDynamicContentItem(string type, Dictionary<string, object> fields, bool publishItem = false, string identifyingFieldName = "Title", bool requireUniqueIdentifyingField = true, bool createUniqueUrl = false, Guid ownerId = new Guid(), bool suppressSecurity = true, bool manualPublishDate = false) {
- //Create blank item
- var dynMgr = DynamicModuleManager.GetManager(DynamicModuleManager.GetDefaultProviderName());
- var itemType = TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
- var item = dynMgr.CreateDataItem(itemType);
- //Set field data
- foreach (var field in fields) {
- item.SetValue(field.Key, field.Value);
- }
- //Title doesn't have to be unique
- var identifyingField = fields[identifyingFieldName].ToString();
- if (requireUniqueIdentifyingField && dynMgr.GetDataItems(itemType).Any(o => o.GetValue<string>(identifyingFieldName).ToLower() == identifyingField.ToLower())) {
- throw new ArgumentException(string.Format("Identifying field value is not unique: '{0}', value: '{1}'", identifyingFieldName, identifyingField));
- }
- //URL must be unique
- var url = Regex.Replace(identifyingField.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
- var itemExists = dynMgr.GetDataItems(itemType).Any(o => o.UrlName == url);
- if (itemExists && !createUniqueUrl) {
- throw new ArgumentException(string.Format("URL is not unique based on identifying field: '{0}', value: '{1}'", identifyingFieldName, identifyingField));
- }
- if (itemExists && createUniqueUrl) {
- //Create unique URL means append "-1" or "-2" etc... until unique
- var modifier = 1;
- while (dynMgr.GetDataItems(itemType).Any(o => o.UrlName == string.Format("{0}-{1}", url, modifier))) {
- modifier++;
- }
- url = string.Format("{0}-{1}", url, modifier);
- }
- item.UrlName = url;
- //Owner - if no ID provided, use logged on user
- if (ownerId == Guid.Empty) {
- var currentUser = SitefinityFunctions.CurrentLoggedInUser();
- item.Owner = currentUser == null ? Guid.Empty : currentUser.Id;
- } else {
- item.Owner = ownerId;
- }
- //Save the master item
- if (!manualPublishDate) { item.PublicationDate = DateTime.Now; }
- item.SetWorkflowStatus(dynMgr.Provider.ApplicationName, "Draft");
- if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = true; }
- dynMgr.SaveChanges();
- if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = false; }
- if (publishItem) {
- //We can now call the following to publish the item
- if (!manualPublishDate)
- {
- var publishedItem = dynMgr.Lifecycle.Publish(item);
- }
- else
- {
- var publishedItem = dynMgr.Lifecycle.PublishWithSpecificDate(item, item.PublicationDate);
- }
- //You need to set appropriate workflow status
- item.SetWorkflowStatus(dynMgr.Provider.ApplicationName, "Published");
- //You need to call SaveChanges() in order for the items to be actually persisted to data store
- if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = true; }
- dynMgr.SaveChanges();
- if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = false; }
- }
- return item;
- }
Advertisement
Add Comment
Please, Sign In to add comment