コード例 #1
0
        public static void SetMetadataFields(ClientContext cc, JObject inputFields, FieldCollection fields, ListItem item)
        {
            foreach (KeyValuePair <string, JToken> inputField in inputFields)
            {
                var field = fields.GetByInternalNameOrTitle(inputField.Key);

                cc.Load(field);
                cc.ExecuteQuery();
                Console.WriteLine(field.TypeAsString);


                if (field.TypeAsString.Equals("TaxonomyFieldType"))
                {
                    var taxKeywordField = cc.CastTo <TaxonomyField>(field);

                    Guid   _id     = taxKeywordField.TermSetId;
                    string _termID = TermHelper.GetTermIdByName(cc, inputField.Value.ToString(), _id);


                    TaxonomyFieldValue termValue = new TaxonomyFieldValue()
                    {
                        Label    = inputField.Value.ToString(),
                        TermGuid = _termID,
                        //WssId = -1
                        //WssId = (int)taxObj["WssId"]
                    };

                    taxKeywordField.SetFieldValueByValue(item, termValue);
                    taxKeywordField.Update();
                }
                else if (field.TypeAsString.Equals("User"))
                {
                    var user = FieldUserValue.FromUser(inputField.Value.ToString());
                    item[inputField.Key] = user;
                }
                else if (field.TypeAsString.Equals("DateTime") && inputField.Value.ToString() != "")
                {
                    string dateTimeStr = inputField.Value.ToString();
                    dateTimeStr          = dateTimeStr.Replace("~t", "");
                    item[inputField.Key] = Convert.ToDateTime(dateTimeStr);
                }
                else if (inputField.Value.ToString() == "")
                {
                    continue;
                }
                else
                {
                    item[inputField.Key] = inputField.Value.ToString();
                }

                // This method works but not practical
                //string termValue = "-1;#" + taxObj["Label"].ToString() + "|" + taxObj["TermGuid"].ToString();
                //item[inputField.Key] = termValue;


                item.SystemUpdate();
                //cc.ExecuteQuery();
            }
        }
コード例 #2
0
        public static void SetMetadataFields(ClientContext cc, List list, JObject inputFields, List <Metadata> fields, ListItem item)
        {
            try
            {
                DateTime dtMin = new DateTime(1900, 1, 1);
                foreach (KeyValuePair <string, JToken> inputField in inputFields)
                {
                    if (inputField.Value == null || inputField.Value.ToString() == "")
                    {
                        //Console.WriteLine(inputField.Key);
                        continue;
                    }


                    var field = fields.Find(f => f.InternalName == inputField.Key);

                    if (field.TypeAsString.Equals("TaxonomyFieldType"))
                    {
                        Field taxField        = list.Fields.GetByInternalNameOrTitle(inputField.Key);
                        var   taxKeywordField = cc.CastTo <TaxonomyField>(taxField);


                        Guid   _id     = taxKeywordField.TermSetId;
                        string _termID = TermHelper.GetTermIdByName(cc, inputField.Value.ToString(), _id);


                        TaxonomyFieldValue termValue = new TaxonomyFieldValue()
                        {
                            Label    = inputField.Value.ToString(),
                            TermGuid = _termID,
                            //WssId = -1
                            //WssId = (int)taxObj["WssId"]
                        };

                        taxKeywordField.SetFieldValueByValue(item, termValue);
                        taxKeywordField.Update();
                    }
                    else if (field.TypeAsString.Equals("User"))
                    {
                        //use stringbuilder
                        var user = FieldUserValue.FromUser(inputField.Value.ToString());
                        item[inputField.Key] = user;
                        Console.WriteLine("Set field " + inputField.Key + " to " + user);
                    }
                    else if (field.TypeAsString.Equals("DateTime"))
                    {
                        string dateTimeStr = inputField.Value.ToString();
                        dateTimeStr = dateTimeStr.Replace("~t", "");
                        if (DateTime.TryParse(dateTimeStr, out DateTime dt))
                        {
                            if (dtMin <= dt)
                            {
                                item[inputField.Key] = dt;
                                Console.WriteLine("Set field " + inputField.Key + "to " + dt);
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        int tokenLength = inputField.Value.Count();

                        if (tokenLength >= 1)
                        {
                            continue;
                        }
                        else
                        {
                            item[inputField.Key] = inputField.Value.ToString();
                            Console.WriteLine("Set " + inputField.Key + " to " + inputField.Value.ToString());
                        }
                    }

                    item.SystemUpdate();
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
コード例 #3
0
        public static Folder CreateDocumentSetWithTaxonomy(ClientContext cc, List list, string sitecontent, string documentSetName, IDictionary <string, string> inputFields, List <Metadata> fields, IDictionary <string, string> taxonomy)
        {
            try
            {
                ContentTypeCollection listContentTypes = list.ContentTypes;
                cc.Load(listContentTypes, types => types.Include(type => type.Id, type => type.Name, type => type.Parent));
                //var result = cc.LoadQuery(listContentTypes.Where(c => c.Name == "document set 2"));
                //string SiteContentName = sitecontent;
                var result = cc.LoadQuery(listContentTypes.Where(c => c.Name == sitecontent));

                cc.ExecuteQuery();

                ContentType targetDocumentSetContentType = result.FirstOrDefault();
                ListItemCreationInformation newItemInfo  = new ListItemCreationInformation();

                newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
                newItemInfo.LeafName             = documentSetName;

                ListItem newListItem = list.AddItem(newItemInfo);
                newListItem["ContentTypeId"] = targetDocumentSetContentType.Id.ToString();
                newListItem.Update();
                cc.ExecuteQuery();

                var clientRuntimeContext = newListItem.Context;
                for (int i = 0; i < taxonomy.Count; i++)
                {
                    var inputField = taxonomy.ElementAt(i);
                    var fieldValue = inputField.Value;

                    var field = list.Fields.GetByInternalNameOrTitle(inputField.Key);
                    cc.Load(field);
                    cc.ExecuteQuery();
                    var taxKeywordField = clientRuntimeContext.CastTo <TaxonomyField>(field);

                    Guid   _id     = taxKeywordField.TermSetId;
                    string _termID = TermHelper.GetTermIdByName(cc, fieldValue, _id);

                    TaxonomyFieldValue termValue = new TaxonomyFieldValue()
                    {
                        Label    = fieldValue.ToString(),
                        TermGuid = _termID,
                    };


                    taxKeywordField.SetFieldValueByValue(newListItem, termValue);
                    taxKeywordField.Update();
                    newListItem.Update();
                }

                DateTime dtMin = new DateTime(1900, 1, 1);
                Regex    regex = new Regex(@"~t.*");
                foreach (KeyValuePair <string, string> inputField in inputFields)
                {
                    if (inputField.Value == null || inputField.Value == "" || inputField.Key.Equals("Modified"))
                    {
                        continue;
                    }


                    string fieldValue = inputField.Value;
                    Match  match      = regex.Match(fieldValue);

                    Metadata field = fields.Find(x => x.InternalName.Equals(inputField.Key));
                    if (field.TypeAsString.Equals("User"))
                    {
                        int uid = SharePointHelper.GetUserId(cc, fieldValue);
                        newListItem[inputField.Key] = new FieldUserValue {
                            LookupId = uid
                        };
                    }
                    //endre hard koding
                    else if (inputField.Key.Equals("Modified_x0020_By") || inputField.Key.Equals("Created_x0020_By") || inputField.Key.Equals("Dokumentansvarlig"))
                    {
                        StringBuilder sb = new StringBuilder("i:0#.f|membership|");
                        sb.Append(fieldValue);
                        newListItem[inputField.Key] = sb;
                    }
                    else if (match.Success)
                    {
                        fieldValue = fieldValue.Replace("~t", "");
                        if (DateTime.TryParse(fieldValue, out DateTime dt))
                        {
                            if (dtMin <= dt)
                            {
                                newListItem[inputField.Key] = dt;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else
                    {
                        newListItem[inputField.Key] = fieldValue;
                    }
                    newListItem.Update();
                }



                Folder folder = newListItem.Folder;
                return(folder);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to create document set");
                Console.WriteLine(ex);
                throw;
            }
        }