2012-03-17 5 views
1

두 개의 목록과 하나의 이벤트 수신기가 있습니다. 이벤트 수신자는 목록의 열을 고유 식별자 (콘텐츠 유형 및 항목 ID의 조합)로 설정합니다. 다른 목록에서 쿼리 문자열과 newform을 사용하여 위의 매우 동일한 고유 식별자로 필드를 설정합니다. 또한 함께 작업하고 싶은 newform에 대한 정보를 수집합니다.항목의 ID를 몰라도 SPItem을 찾아야합니다.

ID를 모르는 상태에서 목록을 쿼리하는 방법은 무엇입니까?하지만 다른 항목을 구분할 수있는 다른 고유 ID가 있으므로 다른 필드를 찾을 수 있도록 SPIEM 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"]; 
,210

감사


답변 :

// 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]; 

답변

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(); 
     } 
    } 
} 
+0

친애하는 Nat, 나는 이미이 문제를 해결했지만 귀하의 대답은 정확한 것입니다. 내가 나를 위해 완벽하게 만들었던 것은 SPListItem item = itemCollection [0]에 의해 처음으로 유일 항목을 선택한 쿼리에 대한 것이 었습니다. – regularroutine

관련 문제