locked
C# code snippet? RRS feed

  • Question

  • I have the following SQL statement that I need to run against a SharePoint 2007 content database. 

    delete from dbo.alllists where tp_id = '{ED7FE381-1140-4657-8734-7B1D784C7946}'

    Since direct manipulation is forbidden I need to do this via code. Any help would be appreciated. My programming skills are a little bit out of date.

    Thanks,

     

    • Edited by Mike Walsh FIN Saturday, February 19, 2011 9:18 AM Bold removed from text. (Most lines) Need removed from Title ? added
    Friday, February 18, 2011 4:47 PM

Answers

  • Essentially....

     

          Guid SiteGuid = new Guid("Your-GUID-HERE...");
          Guid WebGuid = new Guid("Your-GUID-HERE...");
          Guid ListGuid = new Guid("Your-GUID-HERE...");
    
    
          using (SPSite oSite = new SPSite(SiteGuid))
          {
            using (SPWeb oWeb = oSite.OpenWeb(WebGuid))
            {
              oWeb.Lists.Delete(ListGuid);
            }
          }
        
    

    This obviously has no error handling but the concept is there.

    • Marked as answer by Porter Wang Monday, February 21, 2011 1:31 AM
    Friday, February 18, 2011 5:59 PM
  • Much easy way is to use Power Shell.

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    $site = new-Object Microsoft.SharePoint.SPSite('http://mysite/')
    $web = $site.OpenWeb()
    $listGuid = New-Object Guid("ED7FE381-1140-4657-8734-7B1D784C7946");
    $web.Lists.Delete($listGuid);
    $web.Dispose();
    $site.Dispose();
    

    Oleg
    • Marked as answer by Porter Wang Monday, February 21, 2011 1:31 AM
    Friday, February 18, 2011 7:37 PM

All replies

  • Essentially....

     

          Guid SiteGuid = new Guid("Your-GUID-HERE...");
          Guid WebGuid = new Guid("Your-GUID-HERE...");
          Guid ListGuid = new Guid("Your-GUID-HERE...");
    
    
          using (SPSite oSite = new SPSite(SiteGuid))
          {
            using (SPWeb oWeb = oSite.OpenWeb(WebGuid))
            {
              oWeb.Lists.Delete(ListGuid);
            }
          }
        
    

    This obviously has no error handling but the concept is there.

    • Marked as answer by Porter Wang Monday, February 21, 2011 1:31 AM
    Friday, February 18, 2011 5:59 PM
  • Perfect, thanks I'll get it a try on my test server.

     

    DLDevine

    Friday, February 18, 2011 6:24 PM
  • Much easy way is to use Power Shell.

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    $site = new-Object Microsoft.SharePoint.SPSite('http://mysite/')
    $web = $site.OpenWeb()
    $listGuid = New-Object Guid("ED7FE381-1140-4657-8734-7B1D784C7946");
    $web.Lists.Delete($listGuid);
    $web.Dispose();
    $site.Dispose();
    

    Oleg
    • Marked as answer by Porter Wang Monday, February 21, 2011 1:31 AM
    Friday, February 18, 2011 7:37 PM
  • I will agree with Oleg - Powershell is a very good way to do a one time operation like that. If you were looking to write an application that would do that sort of functionality over and over again I would recommend C# then.
    Friday, February 18, 2011 8:41 PM