示例#1
0
        void UpdateDescription(ClientContext clientContext, Guid listId, int listItemId)
        {
            List        list            = clientContext.Web.Lists.GetById(listId);
            ListItem    item            = list.GetItemById(listItemId);
            ContentType itemContentType = item.ContentType;

            clientContext.Load(item);
            clientContext.Load(itemContentType);
            FieldCollection fields = list.Fields;

            clientContext.Load(fields);
            clientContext.ExecuteQuery();

            //if (!item.ContentType.Name.Equals("Rapid Delivery Document Set NL"))
            if (!item.ContentType.Id.ToString().StartsWith("0x0120D52000"))
            {
                return;
            }

            List <LookupFieldSet> lookupDefinitions = new List <LookupFieldSet>();

            LookupFieldSet lookupFieldDefinition = new LookupFieldSet();

            lookupFieldDefinition.LookupField = "BusinessConsultant";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            lookupFieldDefinition             = new LookupFieldSet();
            lookupFieldDefinition.LookupField = "FunctionalConsultant";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            lookupFieldDefinition             = new LookupFieldSet();
            lookupFieldDefinition.LookupField = "ProjectManager";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            Web  lookupListWeb = null;
            List parentList    = null;

            Dictionary <string, string> itemValues = new Dictionary <string, string>();

            foreach (var lookupDefinition in lookupDefinitions)
            {
                if (item[lookupDefinition.LookupField] == null)
                {
                    continue;
                }
                FieldLookupValue itemField        = item[lookupDefinition.LookupField] as FieldLookupValue;
                string           lookupFieldValue = itemField.LookupValue;
                int lookupFieldId = itemField.LookupId;

                FieldLookup lookupField = clientContext.CastTo <FieldLookup>(fields.GetByInternalNameOrTitle(lookupDefinition.LookupField));
                clientContext.Load(lookupField);
                clientContext.ExecuteQuery();

                Guid   parentWeb      = lookupField.LookupWebId;
                string parentListGUID = lookupField.LookupList;

                if (lookupListWeb == null)
                {
                    lookupListWeb = clientContext.Site.OpenWebById(parentWeb);
                }
                if (parentList == null)
                {
                    parentList = lookupListWeb.Lists.GetById(new Guid(parentListGUID));
                    clientContext.Load(parentList);
                    clientContext.ExecuteQuery();
                }

                ListItem lookupListItem = parentList.GetItemById(lookupFieldId);
                clientContext.Load(lookupListItem);
                clientContext.ExecuteQuery();

                foreach (var fieldMap in lookupDefinition.FieldMappings)
                {
                    if (item[fieldMap.ItemField] == null || !item[fieldMap.ItemField].ToString().Equals(lookupListItem[fieldMap.LookupListField].ToString()))
                    {
                        itemValues.Add(fieldMap.ItemField, lookupListItem[fieldMap.LookupListField].ToString());
                    }
                }
            }

            if (!itemValues.Any())
            {
                return;
            }

            foreach (KeyValuePair <string, string> itemValue in itemValues)
            {
                item[itemValue.Key] = itemValue.Value;
            }
            item.Update();
            clientContext.ExecuteQuery();
        }
        void UpdateDescription(ClientContext clientContext, Guid listId, int listItemId)
        {
            List list = clientContext.Web.Lists.GetById(listId);
            ListItem item = list.GetItemById(listItemId);
            ContentType itemContentType = item.ContentType;
            clientContext.Load(item);
            clientContext.Load(itemContentType);
            FieldCollection fields = list.Fields;
            clientContext.Load(fields);
            clientContext.ExecuteQuery();

            //if (!item.ContentType.Name.Equals("Rapid Delivery Document Set NL"))
            if(!item.ContentType.Id.ToString().StartsWith("0x0120D52000"))
                return;

            List<LookupFieldSet> lookupDefinitions = new List<LookupFieldSet>();

            LookupFieldSet lookupFieldDefinition = new LookupFieldSet();
            lookupFieldDefinition.LookupField = "BusinessConsultant";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("BusinessConsultantMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            lookupFieldDefinition = new LookupFieldSet();
            lookupFieldDefinition.LookupField = "FunctionalConsultant";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("FunctionalConsultantMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            lookupFieldDefinition = new LookupFieldSet();
            lookupFieldDefinition.LookupField = "ProjectManager";
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerFirstName", "FirstName"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerLastName", "Title"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerEmail", "Email"));
            lookupFieldDefinition.FieldMappings.Add(new LookupFieldMapping("ProjectManagerMobile", "CellPhone"));
            lookupDefinitions.Add(lookupFieldDefinition);

            Web lookupListWeb = null;
            List parentList = null;

            Dictionary<string, string> itemValues = new Dictionary<string, string>();
            foreach (var lookupDefinition in lookupDefinitions)
            {
                if (item[lookupDefinition.LookupField] == null) continue;
                FieldLookupValue itemField = item[lookupDefinition.LookupField] as FieldLookupValue;
                string lookupFieldValue = itemField.LookupValue;
                int lookupFieldId = itemField.LookupId;

                FieldLookup lookupField = clientContext.CastTo<FieldLookup>(fields.GetByInternalNameOrTitle(lookupDefinition.LookupField));
                clientContext.Load(lookupField);
                clientContext.ExecuteQuery();

                Guid parentWeb = lookupField.LookupWebId;
                string parentListGUID = lookupField.LookupList;

                if (lookupListWeb == null) lookupListWeb = clientContext.Site.OpenWebById(parentWeb);
                if (parentList == null)
                {
                    parentList = lookupListWeb.Lists.GetById(new Guid(parentListGUID));
                    clientContext.Load(parentList);
                    clientContext.ExecuteQuery();
                }

                ListItem lookupListItem = parentList.GetItemById(lookupFieldId);
                clientContext.Load(lookupListItem);
                clientContext.ExecuteQuery();

                foreach(var fieldMap in lookupDefinition.FieldMappings)
                {
                    if (item[fieldMap.ItemField] == null || !item[fieldMap.ItemField].ToString().Equals(lookupListItem[fieldMap.LookupListField].ToString()))
                    {
                        itemValues.Add(fieldMap.ItemField, lookupListItem[fieldMap.LookupListField].ToString());
                    }
                }
            }

            if (!itemValues.Any())
                return;

            foreach(KeyValuePair<string,string> itemValue in itemValues)
            {
                item[itemValue.Key] = itemValue.Value;
            }
            item.Update();
            clientContext.ExecuteQuery();
        }