private object DuplicateValues(SPListItem item, DuplicatePluginConfig config) { if (item == null) throw new ArgumentNullException("item"); log.LogInfo("DUPLICATE on item " + item.FormUrlDisplay()); if (config.DuplicateFields == null) throw new Exception("Configuration has no values to duplicate"); //Teoreticky by nemelo nikdy nastat if (!item.ParentList.ContainsFieldIntName(config.UsedFields)) { throw new SPFieldNotFoundException(item.ParentList, config.UsedFields); } foreach (Dictionary<string, List<string>> dict in config.DuplicateFields) { SPField fromField = item.ParentList.OpenField(dict["FROM-FIELD"].First()); SPField toField = item.ParentList.OpenField(dict["TO-FIELD"].First()); Type fromType = fromField.GetType(); Type toType = toField.GetType(); bool toTypeIsText = ( toType == typeof (SPFieldText) || toType == typeof (SPFieldMultiLineText) ); object valueToStore = item[fromField.InternalName]; if (valueToStore != null) { valueToStore = item[fromField.InternalName].ToString(); //budou urcite zakazane typy do kterych nelze zapisovat if (toType == typeof (SPFieldCalculated) || toType == typeof (SPFieldCalculated)) { log.LogError("Cant copy value from field '" + fromField + "' to field '" + toField + "' of type " + toType); continue; } if (fromType == typeof(SPFieldAttachments)) { } if (fromType == typeof (SPFieldBoolean)) { if (toType == typeof (SPFieldNumber)) valueToStore = valueToStore.ToBool() ? 1 : 0; } if (fromType == typeof(SPFieldAllDayEvent)) { } if (fromType == typeof (SPFieldCalculated)) { if (toTypeIsText) { valueToStore = ( (string) valueToStore ).GetLookupValues(); } if (toType == typeof (SPFieldNumber)) { valueToStore = ( (string) valueToStore ).GetLookupIndex(); } } if (fromType == typeof(SPFieldChoice)) { } if (fromType == typeof(SPFieldComputed)) { } if (fromType == typeof(SPFieldCrossProjectLink)) { } if (fromType == typeof(SPFieldCurrency)) { } if (fromType == typeof(SPFieldDateTime)) { } if (fromType == typeof(SPFieldDecimal)) { } if (fromType == typeof(SPFieldFile)) { } if (fromType == typeof(SPFieldGeolocation)) { } if (fromType == typeof(SPFieldIndex)) { } if (fromType == typeof(SPFieldGuid)) { } if (fromType == typeof(SPFieldLink)) { } if (fromType == typeof (SPFieldLookup)) { if (toTypeIsText) { valueToStore = ( (string) valueToStore ).GetLookupValues(); } } if (fromType == typeof (SPFieldMultiChoice)) { if (toTypeIsText) { List<string> vals = ( valueToStore ?? "" ).ToString().Split(";#").Where(v => !string.IsNullOrEmpty(v)).ToList(); valueToStore = vals.JoinStrings("; "); } } if (fromType == typeof(SPFieldMultiColumn)) { } if (fromType == typeof(SPFieldMultiLineText)) { } if (fromType == typeof(SPFieldNumber)) { } if (fromType == typeof(SPFieldPageSeparator)) { } if (fromType == typeof(SPFieldRecurrence)) { } if (fromType == typeof(SPFieldText)) { } if (fromType == typeof(SPFieldUrl)) { } if (fromType == typeof (SPFieldUser)) { if (toTypeIsText) { List<SPPrincipal> principals = item.Web.GetSPPrincipals(item[fromField.InternalName]); valueToStore = principals.GetNames().JoinStrings(", "); } } } item[toField.InternalName] = valueToStore; } try { item.SystemUpdate(false, false); // bez false bude hazet System.Runtime.InteropServices.COMException (0x81020037) } catch (Exception saveException) { log.LogException(saveException, "Item update problem"); } return "OK"; }
private void SetRightsForItem(SPListItem item, List<RoleItem> roles, bool clear, bool expandUsers) { setRightsLog += item.FormUrlDisplay() + " | ClearRights:" + clear + " | ExpandUsers:" + expandUsers + "\n"; if (clear) item.RemoveRights(); foreach (RoleItem role in roles) { List<SPPrincipal> users = LoadUsers(item, role.For, expandUsers); SPRoleDefinition spRole = item.Web.RoleDefinitions.Cast<SPRoleDefinition>().SingleOrDefault(r => r.Id == role.Role_Name); if (spRole == null) { log.LogError("Could not find permisson level: '" + role + "'"); continue; } item.SetRights(users, spRole, false); setRightsLog += spRole.Name + " : " + users.Select(l => l.LoginNameNormalized()).JoinStrings(", ") + "\n"; } }