Question

I am trying to search a document library for a specific document. I am fairly new to sharepoint and cannot figure out how to retrieve the document.

Below is my code:

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();
      }
    }
  }
}
Was it helpful?

Solution

Try this

  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();
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top