Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to find a SPItem without knowing the ID of the item
    primarykey
    data
    text
    <p>I have two lists and one event receiver. The event receiver sets a column of the list to a unique identifier (combo of the content type and the item id). In the other list I use query string and the newform to set a field to the very same unique identifier above. I also gather some more info in that newform I want to work with.</p> <p>How can I query a list without knowing the ID, but I have another unique id to single out from the other items so I can work with it just as SPitem firstitem below so I can find the other fields.</p> <hr> <p>Getting my first item to get the unique title:</p> <pre><code>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)); </code></pre> <p>this is the unique title I want to query second list with:</p> <pre><code>string uniqueTitle = firstItem["Title"].ToString(); </code></pre> <p>getting my second list up, ready for action:</p> <pre><code>SPList secondList = web.Lists["My Second List"]; </code></pre> <hr> <p>Thanks</p> <hr> <p>ANSWER:</p> <pre><code>// open the second list SPList secondList = web.Lists["My Second List"]; // Query the second list for the item SPQuery myQuery = new SPQuery(); myQuery.Query = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name = \"Title\"/&gt;" + "&lt;Value Type = \"Text\"&gt;" + uniqueTitle + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;"; // 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]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload