C#sharepoint如何从特定视图中检索列表项

时间:2018-08-24 10:59:56

标签: c# sharepoint

我想检索特定视图的所有列表项。现在我可以检索项目了,但是每次我有22000个项目。但是鉴于Main,我只有两个项目。

enter image description here

我的代码基于Stackoverflow中的其他帖子编写:

using (var ctx = new ClientContext("http://sp2.intranet.mmmm.net/LWW/Europe/Warsaw/loc/eng/"))
{
      Web web = ctx.Web;
      var tasksList = web.Lists.GetByTitle("Collaboration-Intracompany");

      ctx.ExecuteQuery();
      Microsoft.SharePoint.Client.View view = tasksList.Views.GetByTitle("Main");
      ctx.Load(view);
      ctx.ExecuteQuery();

      CamlQuery camlQuery1 = new CamlQuery();
      camlQuery1.ViewXml = view.ViewQuery;
      // camlQuery1.ViewXml = @"<View><Query><Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where></Query><ViewFields><FieldRef Name='ID' /></ViewFields></View>";
      //camlQuery1.ViewXml = @"<View><Query><Where><Eq><FieldRef Name=\'DisplayName\' /><Value Type=\'Text\'>Main</Value></Eq></Where></Query></View>";

      ListItemCollection listItems = tasksList.GetItems(camlQuery1);

      ctx.Load(listItems);
      ctx.ExecuteQuery();
}

我当前的ViewXml在下面。它返回具有“ ENG In Progress”的4个项目。但是这4个项目来自不同的观点。如何过滤以仅返回视图主项?

     CamlQuery camlQuery1 = new CamlQuery();

        camlQuery1.ViewXml = "<View Name="{326A70B3-515F-4B92-B8FA-506865D192AF}" DefaultView="TRUE" Type="HTML" TabularView="FALSE" DisplayName="Main" Url="/LWW/Europe/Warsaw/loc/eng/Lists/CollaborationIntracompany/all.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png">
   <ViewFields>
      <FieldRef Name="LinkTitle" />
      <FieldRef Name="Task_x0020_Status" />
      <FieldRef Name="Priority" />
      <FieldRef Name="Assigned_x0020_To2" />
      <FieldRef Name="Deadline" />
      <FieldRef Name="Author" />
      <FieldRef Name="Modified" />
      <FieldRef Name="Activity" />
   </ViewFields>
   <Query>
      <Where>
         <Eq>
            <FieldRef Name="Task_x0020_Status" />
            <Value Type="Text">ENG In Progress</Value>
         </Eq>
      </Where>
   </Query>
   <Aggregations Value="Off" />
   <RowLimit Paged="TRUE">300</RowLimit>
   <Mobile MobileItemLimit="3" MobileSimpleViewField="LinkTitle" />
   <Toolbar Type="Standard" ShowAlways="TRUE" />
   <XslLink Default="TRUE">main.xsl</XslLink>
   <ParameterBindings>
      <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
      <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
   </ParameterBindings>
</View>";

         ListItemCollection listItems = tasksList.GetItems(camlQuery1);

当我删除查询时:

 camlQuery1.ViewXml = ""
                        + "<View Name='{326A70B3-515F-4B92-B8FA-506865D192AF}' DefaultView='TRUE' Type='HTML' TabularView='FALSE' DisplayName='Main' Url='/LWW/Europe/Warsaw/loc/eng/Lists/CollaborationIntracompany/all.aspx' Level='1' BaseViewID='1' ContentTypeID='0x' ImageUrl='/_layouts/images/generic.png'>"
                        + "<ViewFields>"
                         + "<FieldRef Name='LinkTitle' /><FieldRef Name='Task_x0020_Status' /><FieldRef Name='Priority' /><FieldRef Name='Assigned_x0020_To2' /><FieldRef Name='Deadline' /><FieldRef Name='Author' /><FieldRef Name='Modified' /><FieldRef Name='Activity' /></ViewFields>"
                        //  + "<Query><Where><Eq><FieldRef Name='Task_x0020_Status' /><Value Type='Text'>ENG In Progress</Value></Eq></Where></Query>"
                           + "<Aggregations Value='Off' /><RowLimit Paged='TRUE'>300</RowLimit><Mobile MobileItemLimit='3' MobileSimpleViewField='LinkTitle' />"
                           + "<Toolbar Type='Standard' ShowAlways='TRUE' /><XslLink Default='TRUE'>main.xsl</XslLink><ParameterBindings>"
                           + "<ParameterBinding Name='NoAnnouncements' Location='Resource(wss,noXinviewofY_LIST)' />"
                           + "<ParameterBinding Name='NoAnnouncementsHowTo' Location='Resource(wss,noXinviewofY_DEFAULT)' /></ParameterBindings></View>";

它返回300个项目,因为限制是300个。在图上,您可以看到视图Main仅具有4个位置。如何准确获得这4个职位?

2 个答案:

答案 0 :(得分:0)

我以前已经看过一些东西。

尝试

camlQuery1.ViewXml = "<View><Query>" + view.ViewQuery + "</Query></View>";

欢呼声

特鲁兹

答案 1 :(得分:0)

由于CamlQuery.ViewXml Property期望View作为根元素,因此在提供的示例中,该行:

camlQuery1.ViewXml = view.ViewQuery;

需要替换为:

camlQuery1.ViewXml = String.Format("<View><Query>{0}</Query></View>", view.ViewQuery); 

因此,这是一个通用示例,说明如何按视图检索列表项:

   //1.Request view query
   var list = ctx.Web.Lists.GetByTitle(listName);
   var view = list.Views.GetByTitle(viewName);
   ctx.Load(view);
   ctx.ExecuteQuery();

   //2.construct list query from view query and retrieve items 
   var qry = new CamlQuery();
   qry.ViewXml = String.Format("<View><Query>{0}</Query></View>", view.ViewQuery); 
   var items = list.GetItems(qry);
   ctx.Load(items);
   ctx.ExecuteQuery(); 

关于分组

但这是棘手的部分,在提供的屏幕截图中,仅显示了分组的(特别是折叠的)项目。关键是SharePoint GroupBy element的行为不完全符合预期(例如,像SQL中的GROUP BY一样),这意味着即使{ {1}}运算符被应用。

要检索通过列表视图呈现的完全相同的数据,最好使用GroupBy方法,例如:

List.RenderListData