Пример #1
0
        public static void AddNewListItem(CsvRecord record, List spList, ClientContext clientContext)
        {
            Dictionary <string, object> itemFieldValues = new Dictionary <string, object>();

            //Use reflection to iterate through the record's properties
            PropertyInfo[] properties = typeof(CsvRecord).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object propValue = property.GetValue(record, null);
                if (!String.IsNullOrEmpty(propValue.ToString()))
                {
                    Field matchingField = spList.Fields.GetByInternalNameOrTitle(property.Name);
                    clientContext.Load(matchingField);
                    clientContext.ExecuteQuery();

                    switch (matchingField.FieldTypeKind)
                    {
                    case FieldType.User:
                        FieldUserValue userFieldValue = GetUserFieldValue(propValue.ToString(), clientContext);
                        if (userFieldValue != null)
                        {
                            itemFieldValues.Add(matchingField.InternalName, userFieldValue);
                        }
                        else
                        {
                            throw new Exception("User field value could not be added: " + propValue.ToString());
                        }
                        break;

                    case FieldType.Lookup:
                        FieldLookupValue lookupFieldValue = GetLookupFieldValue(propValue.ToString(),
                                                                                ConfigurationManager.AppSettings["LookupListName"].ToString(),
                                                                                clientContext);
                        if (lookupFieldValue != null)
                        {
                            itemFieldValues.Add(matchingField.InternalName, lookupFieldValue);
                        }
                        else
                        {
                            throw new Exception("Lookup field value could not be added: " + propValue.ToString());
                        }
                        break;

                    case FieldType.Invalid:
                        switch (matchingField.TypeAsString)
                        {
                        default:
                            //Code for publishing site columns not implemented
                            continue;
                        }

                    default:
                        itemFieldValues.Add(matchingField.InternalName, propValue);
                        break;
                    }
                }
            }

            //Add new item to list
            ListItemCreationInformation creationInfo = new ListItemCreationInformation();
            ListItem oListItem = spList.AddItem(creationInfo);

            foreach (KeyValuePair <string, object> itemFieldValue in itemFieldValues)
            {
                //Set each field value
                oListItem[itemFieldValue.Key] = itemFieldValue.Value;
            }
            //Persist changes
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
Пример #2
0
        private static void AddNewListItem(CsvRecord record, List spList, ClientContext clientContext)
        {
            //Instantiate dictionary to temporarily store field values
            Dictionary<string, object> itemFieldValues = new Dictionary<string, object>();
            //Use reflection to iterate through the record's properties
            PropertyInfo[] properties = typeof(CsvRecord).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                //Get property value
                object propValue = property.GetValue(record, null);
                //Only set field if the property has a value
                if (!String.IsNullOrEmpty(propValue.ToString()))
                {
                    //Get site column that matches the property name
                    //ASSUMPTION: Your property names match the internal names of the corresponding site columns
                    Field matchingField = spList.Fields.GetByInternalNameOrTitle(property.Name);
                    clientContext.Load(matchingField);
                    clientContext.ExecuteQuery();

                    //Switch on the field type
                    switch (matchingField.FieldTypeKind)
                    {
                        case FieldType.User:
                            FieldUserValue userFieldValue = GetUserFieldValue(propValue.ToString(), clientContext);
                            if (userFieldValue != null)
                                itemFieldValues.Add(matchingField.InternalName, userFieldValue);
                            else
                                throw new Exception("User field value could not be added: " + propValue.ToString());
                            break;
                        case FieldType.Lookup:
                            FieldLookupValue lookupFieldValue = GetLookupFieldValue(propValue.ToString(),
                                ConfigurationManager.AppSettings["LookupListName"].ToString(),
                                clientContext);
                            if (lookupFieldValue != null)
                                itemFieldValues.Add(matchingField.InternalName, lookupFieldValue);
                            else
                                throw new Exception("Lookup field value could not be added: " + propValue.ToString());
                            break;
                        case FieldType.Invalid:
                            switch (matchingField.TypeAsString)
                            {
                                case "TaxonomyFieldType":
                                    TaxonomyFieldValue taxFieldValue = GetTaxonomyFieldValue(propValue.ToString(), matchingField, clientContext);
                                    if (taxFieldValue != null)
                                        itemFieldValues.Add(matchingField.InternalName, taxFieldValue);
                                    else
                                        throw new Exception("Taxonomy field value could not be added: " + propValue.ToString());
                                    break;
                                default:
                                    //Code for publishing site columns not implemented
                                    continue;
                            }
                            break;
                        default:
                            itemFieldValues.Add(matchingField.InternalName, propValue);
                            break;
                    }
                }
            }

            //Add new item to list
            ListItemCreationInformation creationInfo = new ListItemCreationInformation();
            ListItem oListItem = spList.AddItem(creationInfo);

            foreach (KeyValuePair<string, object> itemFieldValue in itemFieldValues)
            {
                //Set each field value
                oListItem[itemFieldValue.Key] = itemFieldValue.Value;
            }
            //Persist changes
            oListItem.Update();
            clientContext.ExecuteQuery();
        }