Tell me more ×
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

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

You can also do it this way

    try
    {
SPListItem item = web.GetListItem(itemUrl);
item.Delete()
    }
    catch(Exception ex)
    {

    }

If the url is not valid FileNotFoundException will be thrown

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.