如何使用客户端对象模型C#用新值更新查找字段 [英] How to update a lookup field with new value using client object model c#

查看:53
本文介绍了如何使用客户端对象模型C#用新值更新查找字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

           我在列表中有查找字段,我想更改查找字段的外观值,这是我的代码.现有值是"Jhon"我想将其替换为"Clark"

             i have lookup field in a list , i want to change the look value of lookup field here is my code. existing value is "Jhon" i want to replace it with "Clark"

 public void load()
        {
            try
            {
                using (ClientContext context = new ClientContext("http://IP Address/Leaves/"))
                {
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    context.Credentials = new NetworkCredential("username", "********");
                    Web webObj = context.Web;
                    List listObj = webObj.Lists.GetByTitle("Leaves");
                    //CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(200000);

                    DateTime dtStart = Convert.ToDateTime("11/14/2016 4:25:06 PM");
                    string dtStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ssZ");
                    string dtEndDate = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");

                    Microsoft.SharePoint.Client.CamlQuery camlquery = new CamlQuery();
                    camlquery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtStartDate + "</Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtEndDate + "</Value></Leq></And></Where>";
                    ListItemCollection listitemcoll = listObj.GetItems(camlquery);

                    context.Load(listitemcoll);
                    context.ExecuteQuery();
                    if (listitemcoll.Count != 0)
                    {

                        foreach (ListItem item in listitemcoll)
                        {
                            int ID = Convert.ToInt32(item["ID"]);
                            var listItem = listObj.GetItemById(Convert.ToInt32(ID));
                            context.Load(listItem);
                            context.ExecuteQuery();
                            FieldLookupValue lookup = item["Employee"] as FieldLookupValue;
                            string lvalue = lookup.LookupValue; \\ existing value is "jhone" new value "Clark"
                         
                            int lId = lookup.LookupId;
                           

                            lookup.LookupId = lId;
                            item["Employee"] = lookup;
                            item.Update();
                            context.ExecuteQuery();

                         
                        }
                       
                    }
                    else
                    {
                        //write messages to client - no data is present
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


推荐答案

Razim,

Hi Razim,

要使用客户端对象模型更新查找字段,需要查找列表中项目的ID.然后,可以使用此ID更新列表项的FieldLookupValue.

To update a lookup field using client side object model, ID of the item from the lookup list is required. This id can then be used to update the listitem's FieldLookupValue.

请参考以下文章

尝试一下: https://rmanimaran.wordpress.com/2012/07/16/update-lookup-field-using-client-object-model/

Try This :  https://rmanimaran.wordpress.com/2012/07/16/update-lookup-field-using-client-object-model/

我希望这会有所帮助.

最好的问候,

Ramesh


这篇关于如何使用客户端对象模型C#用新值更新查找字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆