2

我正在尝试在文档库中搜索特定文档。我对 sharepoint 还很陌生,无法弄清楚如何检索文档。

下面是我的代码:

private void button12_Click(object sender, EventArgs e)
{
  using (var site = new SPSite(SiteUrl))
  {
    if (SiteUrl != null)
    {
      using (SPWeb web = site.OpenWeb())
      {
        SPList list = web.Lists["Documents"];

        if (list != null)
        {
          foreach (SPListItem item in list.Items)
          {
            if (item.Name.Any() == textBox1.ToString().Any())
              listBox1.Items.Add("Document Found");
            else
              listBox1.Items.Add("Cannot Find Document");
          }
          web.Close();
        }
        site.Close();
      }
    }
  }
}
4

1 回答 1

2

尝试这个

  using (var site = new SPSite(SiteUrl))
        {
            if (SiteUrl != null)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Shared Documents"];

                    if (list != null)
                    {
                        int i = 1;
                        foreach (SPListItem item in list.Items)
                        {

                            if (item["Name"].ToString() == TextBox1.Text)
                            {
                                Label1.Text = "Document Found";
                                break;
                            }
                            else if (list.Items.Count == i)
                            {
                                Label1.Text = "Cannot Find Document";
                            }
                            i++;
                        }
                        web.Close();
                    }
                    site.Close();
                }
            }
        }
于 2012-11-28T10:45:13.017 回答