SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

At the moment I am trying to delete list item file using this code,

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(properties.WebUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPListItem Item = web.GetListItem(ListURL + fileName);
                        Item.Delete();
                    }
                }
            });

but I need to do validations if item exists or not, I know I can iterate list items but is there any easy way of checking if item exists using URL of item.

share|improve this question
up vote 2 down vote accepted

Use SPWeb.GetFile followed by SPFile.Exists

SPFile file = web.GetFile(fullUrl);
if(file.Exists)
{
    ...
}
share|improve this answer
foreach (SPListItem listItem in list.Items)
{
  string s= listItem.Name;
  SPFolderCollection Foldcoll = list.RootFolder.SubFolders;
  foreach (SPFolder RootFold in Foldcoll)
  {
      var file = listItem.Web.GetFile(listItem.UniqueId);
      if (file.Exists)
      {
          file.delete();                                 
      }     
  }
}           
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.