Stephen2

Sitefinity - CreateDynamicItem

May 21st, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1.         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) {
  2.             //Create blank item
  3.             var dynMgr = DynamicModuleManager.GetManager(DynamicModuleManager.GetDefaultProviderName());
  4.             var itemType = TypeResolutionService.ResolveType(string.Concat("Telerik.Sitefinity.DynamicTypes.Model.", type));
  5.             var item = dynMgr.CreateDataItem(itemType);
  6.  
  7.             //Set field data
  8.             foreach (var field in fields) {
  9.                 item.SetValue(field.Key, field.Value);
  10.             }
  11.  
  12.             //Title doesn't have to be unique
  13.             var identifyingField = fields[identifyingFieldName].ToString();
  14.  
  15.             if (requireUniqueIdentifyingField && dynMgr.GetDataItems(itemType).Any(o => o.GetValue<string>(identifyingFieldName).ToLower() == identifyingField.ToLower())) {
  16.                 throw new ArgumentException(string.Format("Identifying field value is not unique: '{0}', value: '{1}'", identifyingFieldName, identifyingField));
  17.             }
  18.  
  19.             //URL must be unique
  20.             var url = Regex.Replace(identifyingField.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
  21.             var itemExists = dynMgr.GetDataItems(itemType).Any(o => o.UrlName == url);
  22.  
  23.             if (itemExists && !createUniqueUrl) {
  24.                 throw new ArgumentException(string.Format("URL is not unique based on identifying field: '{0}', value: '{1}'", identifyingFieldName, identifyingField));
  25.             }
  26.  
  27.             if (itemExists && createUniqueUrl) {
  28.                 //Create unique URL means append "-1" or "-2" etc... until unique
  29.                 var modifier = 1;
  30.  
  31.                 while (dynMgr.GetDataItems(itemType).Any(o => o.UrlName == string.Format("{0}-{1}", url, modifier))) {
  32.                     modifier++;
  33.                 }
  34.  
  35.                 url = string.Format("{0}-{1}", url, modifier);
  36.             }
  37.  
  38.             item.UrlName = url;
  39.            
  40.             //Owner - if no ID provided, use logged on user
  41.             if (ownerId == Guid.Empty) {
  42.                 var currentUser = SitefinityFunctions.CurrentLoggedInUser();
  43.                 item.Owner = currentUser == null ? Guid.Empty : currentUser.Id;
  44.             } else {
  45.                 item.Owner = ownerId;
  46.             }
  47.  
  48.             //Save the master item
  49.             if (!manualPublishDate) { item.PublicationDate = DateTime.Now; }
  50.             item.SetWorkflowStatus(dynMgr.Provider.ApplicationName, "Draft");
  51.             if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = true; }
  52.             dynMgr.SaveChanges();
  53.             if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = false; }
  54.  
  55.             if (publishItem) {
  56.                 //We can now call the following to publish the item
  57.                 if (!manualPublishDate)
  58.                 {
  59.                     var publishedItem = dynMgr.Lifecycle.Publish(item);
  60.                 }
  61.                 else
  62.                 {
  63.                     var publishedItem = dynMgr.Lifecycle.PublishWithSpecificDate(item, item.PublicationDate);
  64.                 }
  65.  
  66.                 //You need to set appropriate workflow status
  67.                 item.SetWorkflowStatus(dynMgr.Provider.ApplicationName, "Published");
  68.  
  69.                 //You need to call SaveChanges() in order for the items to be actually persisted to data store
  70.                 if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = true; }
  71.                 dynMgr.SaveChanges();
  72.                 if (suppressSecurity) { dynMgr.Provider.SuppressSecurityChecks = false; }
  73.             }
  74.  
  75.             return item;
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment