Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment
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
add comment

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.