需要在不知道项目ID的情况下找到SPItem

时间:2012-03-17 16:03:09

标签: sharepoint

我有两个列表和一个事件接收器。事件接收器将列表的列设置为唯一标识符(内容类型和项目ID的组合)。 在另一个列表中,我使用查询字符串和newform将字段设置为上面相同的唯一标识符。我还收集了一些我希望使用的新形式的信息。

如何在不知道ID的情况下查询列表,但是我有另外一个唯一ID来从其他项中挑出来,所以我可以像下面的SPitem firstitem一样使用它,这样我就可以找到其他字段。


获取我的第一个项目以获得唯一标题:

SPSite currentSite = SPContext.Current.Site;
SPWeb web = currentSite.OpenWeb();

string queryString = Request.QueryString["uniqueID"];
SPList firstList = web.Lists["My First List"];
SPItem firstItem = firstList.Items.GetItemById(Convert.ToInt32(queryString));

这是我想要查询第二个列表的唯一标题:

string uniqueTitle = firstItem["Title"].ToString();

获取我的第二个列表,准备采取行动:

SPList secondList = web.Lists["My Second List"];

由于


解答:

// open the second list
SPList secondList = web.Lists["My Second List"];

// Query the second list for the item
SPQuery myQuery = new SPQuery();
myQuery.Query = "<Where><Eq><FieldRef Name = \"Title\"/>" +
"<Value Type = \"Text\">" + uniqueTitle +
"</Value></Eq></Where>";

// Get a collection of the second list items selected by the query and pick the first (0) value as it should only return one!
SPListItemCollection secondlistItemCol = secondList.GetItems(myQuery);
SPListItem secondItem = secondlistItemCol[0];

1 个答案:

答案 0 :(得分:2)

您需要使用GetItems command with an SPQuery object。使用下面的MS代码示例进行整理并不算太复杂。

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Build a query.
               SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                              "</OrderBy>");                    

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.

               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks";
               SPList list = web.GetList(listUrl);
               SPListItemCollection items = list.GetItems(query);

               // Print a report header.
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                  "Assigned To", "Task", "Due Date", "Priority");

               // Print the details.
               foreach (SPListItem item in items)
               {
                  Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                     item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Priority"]);
               }
            }
         }
         Console.ReadLine();
      }
   }
}