在GridView中绑定Sharepoint列表数据 [英] Bind Sharepoint list data in gridview

查看:67
本文介绍了在GridView中绑定Sharepoint列表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采用了以下方法,但是这会使应用程序运行缓慢.请通过另一种方法帮助我.

公共列表< EmailTool> GetData(字符串ID)
        {
        列表< EmailTool>结果=新的List< EmailTool>();

        使用(var clientContext = this.CreateClientContext())
        {
            #region A
            列表A = clientContext.Web.Lists.GetByTitle("A");

            CamlQuery查询= new CamlQuery();
            query.ViewXml = string.Format(<< View>" +
                << Query>" +
                << Where>" +
                < Eq>" +
                << FieldRef Name ='Id'/> " +
                 << Value Type ='整数'> {0}</Value>" +
                </Eq>" +
                </Where>" +
                </查询" +
                </View>",Id);

            ListItemCollection nomItems = A.GetItems(query);
             clientContext.Load(A);
            clientContext.Load(nomItems);
            clientContext.ExecuteQuery();

            foreach(nomItems中的ListItem项目)
            {

                FieldUserValue [] nom =(FieldUserValue [])item ["People"];

                foreach(FieldUserValue in nom)
                {
                    EmailTool工具=新的EmailTool();
                    tool.Name = it.LookupValue;
                    tool.EmailAddress = it.Email;
                    results.Add(tool);
                }

            }
            #endregion A
            #region B
            列表B = clientContext.Web.Lists.GetByTitle("B");
            CamlQuery queryB = new CamlQuery();
            queryB.ViewXml = string.Format(< View>< Query>< Where>" +
                                    << Eq>< FieldRef Name ='Id'LookupId ='TRUE'/>" +
                                    <<值类型='查找'> {0}</值</Eq>" +
                            </Where></Query></View>'',Id);

            ListItemCollection nomItems1 = B.GetItems(queryB);
            clientContext.Load(B);
            clientContext.Load(nomItems1);
            clientContext.ExecuteQuery();

            foreach(nomItems1中的ListItem项)
            {
                EmailTool tool1 =新的EmailTool();
                tool1.Name = item ["name"].ToString();
                tool1.EmailAddress = item ["email"].ToString();
                    results.Add(tool1);
            }
            #endregion

        }
        返回结果;

    } 



解决方案

Shraddha,

以下是您可以提高性能的方法-

1.从代码中删除  clientContext.Load(A)和clientContext.Load(B),因为您只关注列表项而不是列表.

2.除了请求所有字段,您还可以仅请求要使用的字段.所以要实现它替换-

clientContext.load(nonitems)- ctx.Load(nomItems,includes => includes.Include(i => i ["People"])));

clientContext.load(nonitems1)- ctx.Load(nomItems,includes => include.Include(i => i ["name"],i => i [;电子邮件"]]

));

下面是一些链接供您参考-

https://sharepoint.stackexchange.com/questions/89634/syntax-for- includes-fields-dynamically-in-csom-query

如果您的ID字段为ootb,则只需通过传递项目ID即可使用GetItembyID方法,而无需使用CAML查询.


 I have two SharePoint two lists- A and B. A has two columns- Id and People(Person or Group). B has three columns - Id(lookup from A list),name,email. In list A, a single item can have multiple person or group in the People column.The requirement is to fetch the email address of the people and bind it in individual rows of the gridview.

I have taken the following approach.But it makes the application very slow.Please help me with another approach.

public List<EmailTool> GetData(string Id)
        {
        List<EmailTool> results = new List<EmailTool>(); 

        using (var clientContext = this.CreateClientContext())
        {
            #region A
            List A= clientContext.Web.Lists.GetByTitle("A");

            CamlQuery query = new CamlQuery();
            query.ViewXml = string.Format("<View>"+
                "<Query>"+
                "<Where>"+
                "<Eq>"+
                "<FieldRef Name='Id'/> " +
                 "<Value Type='Integer'>{0}</Value>"+
                "</Eq>" +
                "</Where>"+
                "</Query>"+
                "</View>",Id);

            ListItemCollection nomItems= A.GetItems(query);
             clientContext.Load(A);
            clientContext.Load(nomItems);
            clientContext.ExecuteQuery();

            foreach (ListItem item in nomItems)
            {

                FieldUserValue[] nom = (FieldUserValue[])item["People"];

                foreach(FieldUserValue it in nom)
                {
                    EmailTool tool = new EmailTool();
                    tool.Name = it.LookupValue;                        
                    tool.EmailAddress = it.Email;                        
                    results.Add(tool); 
                }                    

            }
            #endregion A
            #region B
            List B= clientContext.Web.Lists.GetByTitle("B");
            CamlQuery queryB = new CamlQuery();
            queryB.ViewXml = string.Format("<View><Query><Where>" +
                                    "<Eq><FieldRef Name='Id' LookupId='TRUE'/>" +
                                    "<Value Type='Lookup'>{0}</Value></Eq>" +
                            "</Where></Query></View>",Id);

            ListItemCollection nomItems1 = B.GetItems(queryB);
            clientContext.Load(B);
            clientContext.Load(nomItems1);
            clientContext.ExecuteQuery();

            foreach (ListItem item in nomItems1)
            {
                EmailTool tool1 = new EmailTool();
                tool1.Name = item["name"].ToString();
                tool1.EmailAddress = item["email"].ToString();
                    results.Add(tool1);
            }
            #endregion 

        }
        return results;

    }



解决方案

Hi Shraddha,

Below are the things you can do to improve the performance-

1. Remove the  clientContext.Load(A) and clientContext.Load(B) from your code because you are only focused on the list items not list.

2. Instead of requesting all the fields you can request only the fields you wanna work with. So to achieve it replace-

clientContext.load(nonitems) -- ctx.Load(nomItems, includes => includes.Include(i => i["People"]));

clientContext.load(nonitems1) -- ctx.Load(nomItems, includes => includes.Include(i => i["name"],i=>i["email"]

));

Below are some links for your reference-

https://sharepoint.stackexchange.com/questions/89634/syntax-for-including-fields-dynamically-in-csom-query

If your ID field is ootb then you can simply use GetItembyID method by passing the item ID, no need to use CAML query.


这篇关于在GridView中绑定Sharepoint列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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