0

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.

2 Answers 2

2

Use SPWeb.GetFile followed by SPFile.Exists

SPFile file = web.GetFile(fullUrl);
if(file.Exists)
{
    ...
}
0
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();                                 
      }     
  }
}           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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