2010-12-10 54 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'";