2010-12-10 56 views
0

我需要獲取某個項目的GUID。我知道SiteURL,列表名稱和項目名稱。我如何得到這個項目導致使用list.items中的splistitem li並循環每個項目(如果li.name == myitemname然後分配strguid = li.uniqueid.tostring()我真的需要foreach循環嗎?! (foreach可能過度使用)

有沒有辦法使用foreach循環,因爲我知道該項目的名稱?這將節省時間來循環項目

+0

SPList.GetItems(SPQuery)。我想我需要構建我的查詢。但它使用listcollection,所以再次通過一切...嗯 – 2010-12-10 19:34:55

+0

名稱?你是說標題? – Ryan 2010-12-10 22:44:58

回答

1

如果你正在splistcollection中的唯一項目上創建一個spquery,你將只返回一個你可以獲得的元素foreach

0
using (SPSite site = new SPSite(SiteURL)) 
      { 
       using (SPWeb web = site.OpenWeb()) 
       { 
        SPList apps = web.GetList(web.Url + "/Lists/MyList"); 
        SPQuery query = new SPQuery(); 
        query.Query = String.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='String'>{0}</Value></Eq></Where>", _title); 
        items = apps.GetItems(query); 
        if(items.Count > 0) 
         { 
         Guid id = items[0].UniqueId; 
         } 
       } 
      } 
+0

qry.ViewAttributes =「Scope ='RecursiveAll'」;或 query.ViewAttributes =「Scope = \」遞歸\「」; – 2010-12-14 18:06:19

0

工作,但我有11個文件,他們都有相同的文件名「license.txt」,他們在同一個文檔圖書館。 1文件「license.txt」位於文檔庫內的根文檔庫和10個不同文件夾中的其他10位。那麼如果我正在尋找Demo文件夾中的license.txt文件呢?

此代碼有效,但僅在根目錄下找到license.txt。 private void btnGetFileGuid_Click(object sender, EventArgs e) { using (SPSite site = new SPSite("https://www.abc.com/sites/Software")) { using (SPWeb web = site.OpenWeb()) { SPList spList = web.Lists["Auto Cad"]; string fileName = "license.txt"; SPQuery query = new SPQuery(); query.Query="<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>" + fileName + "</Value></Eq></Where>"; SPListItemCollection items = spList.GetItems(query); if (items.Count > 0) { Guid id = items[0].UniqueId; lblGuid.Text = id.ToString(); } } } }

+0

有沒有人能夠解決這個問題?我在同一文檔庫中的不同文件夾中有超過10個項目。它沒有找到任何一個。我認爲它只適用於如果該項目在文檔庫的根目錄並且只計算這一項目。 – 2010-12-13 20:23:10

0

嘗試以下,它似乎工作。

query.ViewAttributes = "Scope=\"Recursive\""; 

query.ViewAttributes = "Scope='RecursiveAll'";