private void SetFieldUser(SPListItem item, SPWeb web, object valor, PropertyInfo prop, bool addPrefix)
        {
            var userList = (List <UserSP>)valor;
            var result   = new StringBuilder();

            foreach (var userd in userList)
            {
                if (!string.IsNullOrEmpty(result.ToString()))
                {
                    result.Append(";");
                }
                result.Append(userd.Key);
            }
            if (userList.Count == 1)
            {
                item[GetInternalName(prop.Name, addPrefix)] = !string.IsNullOrEmpty(result.ToString())
                    ? result.ToString()
                    : string.Empty;
            }
            else
            {
                Logger.Warn("Estoy insertando un multiusuario");
                var usercollection = new SPFieldUserValueCollection();
                usercollection.AddRange(
                    userList.Select(userd => new SPFieldUserValue(web, Convert.ToInt32(userd.Key), userd.LoginName)));
                item[GetInternalName(prop.Name, addPrefix)] = usercollection;
            }
        }
        private SPFieldUserValueCollection GetManagers(SPListItem item)
        {
            SPFieldUserValueCollection fieldValues = new SPFieldUserValueCollection();

            if (item["Responsible"] != null)
            {
                SPFieldUserValue userValue = new SPFieldUserValue(item.Web, item["Responsible"].ToString());
                fieldValues.Add(userValue);
            }

            SPFieldUser field = item.Fields["Person in charge"] as SPFieldUser;

            if (field != null && item["Person in charge"] != null)
            {
                SPFieldUserValueCollection picFieldValues = field.GetFieldValue(item["Person in charge"].ToString()) as SPFieldUserValueCollection;
                fieldValues.AddRange(picFieldValues);
            }

            if (item["Approver"] != null)
            {
                SPFieldUserValue userValue = new SPFieldUserValue(item.Web, item["Approver"].ToString());
                fieldValues.Add(userValue);
            }

            string secondApprover = GetSecondApprover(item);

            if (secondApprover != string.Empty && item[secondApprover] != null)
            {
                SPFieldUserValue userValue = new SPFieldUserValue(item.Web, item[secondApprover].ToString());
                fieldValues.Add(userValue);
            }

            return(fieldValues);
        }
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            try
            {
                using (var site = new SPSite(properties.SiteId, SPUserToken.SystemAccount))
                {
                    using (var web = site.OpenWeb(properties.Web.ID))
                    {
                        var numerator  = new Common.Numerator(web);
                        var nextNumber = numerator.GetNextContractNumber();

                        var origFiring = EventFiringEnabled;
                        try
                        {
                            EventFiringEnabled = false;
                            var item = properties.List.GetItemById(properties.ListItemId);

                            item[Fields.ContractNumber] = nextNumber;
                            item[Fields.ContractName]   = nextNumber;

                            item[Fields.ContractInitiator] = new SPFieldUserValue(properties.Web,
                                                                                  properties.CurrentUserId, string.Empty);

                            var allApprovers = GetAllApproversMembers(web);
                            var usersValue   = new SPFieldUserValueCollection();
                            usersValue.AddRange(allApprovers.Select(a => new SPFieldUserValue(web, a.ID, a.Name)));

                            item[Fields.ContractApprovers] = usersValue;
                            //var group = web.SiteGroups[]

                            item.SystemUpdate(false);
                        }
                        finally
                        {
                            EventFiringEnabled = origFiring;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }
        public object ToSpValue(object value)
		{
			if (value == null)
				return null;

			if (!Field.AllowMultipleValues)
			{
				var userInfo = value as UserInfo;

				return new SPFieldUserValue(Field.ParentList.ParentWeb, userInfo.Id, userInfo.Login);
			}

			var userInfos = (IEnumerable<UserInfo>)value;

			var fieldValues = new SPFieldUserValueCollection();
			fieldValues.AddRange(userInfos.Select(userInfo => new SPFieldUserValue(Field.ParentList.ParentWeb, userInfo.Id, userInfo.Login)));

			return fieldValues;
		}
Beispiel #5
0
        private void BuildResourceTable()
        {
            Dictionary <string, object[]> usersDict = GetSiteUsers();

            Dictionary <string, Dictionary <string, int> > lookupFieldDict;
            Dictionary <string, SPFieldType> fieldDict = GetListFields(out lookupFieldDict);

            foreach (DataRow row in _dtResources.Rows)
            {
                if (!this.IsImportCancelled)
                {
                    foreach (DataColumn column in _dtResources.Columns)
                    {
                        string col   = column.ColumnName;
                        object value = row[col];
                        string val   = string.Empty;

                        if (value != null && value != DBNull.Value)
                        {
                            val = value.ToString();
                        }

                        if (!string.IsNullOrEmpty(val))
                        {
                            if (fieldDict.ContainsKey(col))
                            {
                                try
                                {
                                    if (col.Equals("ResourceLevel"))
                                    {
                                        if (!string.IsNullOrEmpty(val))
                                        {
                                            value = lookupFieldDict["ResourceLevel"][val];
                                        }

                                        row[col] = value;
                                        continue;
                                    }

                                    if (col.Equals("Permissions"))
                                    {
                                        if (!string.IsNullOrEmpty(val))
                                        {
                                            IEnumerable <string> permissions =
                                                val.Split(',')
                                                .Select(
                                                    p =>
                                                    lookupFieldDict["Permissions"][p.Trim()].ToString(
                                                        CultureInfo.InvariantCulture));
                                            value = string.Join(",", permissions.ToArray());
                                        }

                                        row[col] = value;
                                        continue;
                                    }

                                    switch (fieldDict[col])
                                    {
                                    case SPFieldType.Boolean:
                                        if (val.Equals("0"))
                                        {
                                            value = false;
                                        }
                                        else if (val.Equals("1"))
                                        {
                                            value = true;
                                        }
                                        break;

                                    case SPFieldType.DateTime:
                                        value = DateTime.FromOADate(Convert.ToDouble(val));
                                        break;

                                    case SPFieldType.User:
                                        if (val.Contains(","))
                                        {
                                            var collection = new SPFieldUserValueCollection();
                                            collection.AddRange(
                                                val.Split(',')
                                                .Select(u => u.Trim())
                                                .Select(uv => GetUserValue(usersDict, uv)));
                                            value = collection;
                                        }
                                        else
                                        {
                                            value = GetUserValue(usersDict, val);
                                        }
                                        break;

                                    case SPFieldType.Lookup:
                                        //if (val.Contains(","))
                                        //{
                                        //    var collection = new SPFieldLookupValueCollection();
                                        //    collection.AddRange(
                                        //        val.Split(',')
                                        //            .Select(v => v.Trim())
                                        //            .Select(lv => new SPFieldLookupValue(lookupFieldDict[col][lv], lv)));
                                        //    value = collection;
                                        //}
                                        //else
                                        //{
                                        try
                                        {
                                            value = new SPFieldLookupValue(lookupFieldDict[col][val], val);
                                        }
                                        catch { throw new Exception(string.Format(" Value : ({0}) does not exists.", val)); }
                                        //}
                                        break;
                                    }
                                }
                                catch (Exception e)
                                {
                                    string message = string.Format("Resource: {0} (ID: {1}). Field: {2} Error: {3}",
                                                                   row["Title"], row["ID"], col, e.Message);
                                    LogImportMessage(message, 2);
                                }
                            }
                        }

                        row[col] = value;
                    }
                }
                else
                {
                    break;
                }
            }
        }