So for the last 5 days I have been working on writting automation to allow an ISP to create and Manage Share Point (WSS) sites. 

Share Point ships with  _vti_adm/admin.asmx web service for creating and deleting sites.  The problem with the create method is that it does not allow you to set the QuotaTemplate or the AnonymousState.

So it was off to create my own Web service.  WSS v2 was not designed to be used in the hoster environment.  I have been told that v3 will fix many of the issues that I have.  V2 does not have a remote way to manage Sites outside of the Admin Page.  So remote automation is tricky.

How my automation is designed so a single server manages automation for the system.  Share Point does not work remotly so you have to write a web service that runs on the SP server and call it from your automation code.

The issue first was

<exception>
System.Exception: Microsoft.SharePoint.SPException: Cannot complete this action.

Please try again. ---&gt; System.Runtime.InteropServices.COMException (0x80004005): Cannot complete this action.

Please try again.
at Microsoft.SharePoint.Library.SPRequestInternalClass.DeleteSite(String bstrUrl, Boolean bDeleteADAccounts)
at Microsoft.SharePoint.Library.a.c(String A_0, Boolean A_1)
--- End of inner exception stack trace ---
at Microsoft.SharePoint.Library.a.c(String A_0, Boolean A_1)
at Microsoft.SharePoint.SPSite.Delete()
at Service.deleteSTSSite(String strSiteName)
at Service.deleteSTSSite(String strSiteName)
at Service.deleteSite(String strSiteName, String strUserName, String strPassword)

 </exception>

<code>
Private Function deleteSTSSite(ByVal strSiteName As String) As Boolean
Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
Dim uri As New Uri(("http://" & strSiteName))
Try
Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
Dim site As SPSite = VS.Sites.Item(("http://" & strSiteName))
site.Delete()
Catch ex As Exception
Throw New Exception(ex.ToString)
'Return False
End Try

Return True
End Function
</code>

The fix was in http://support.microsoft.com/default.aspx?scid=kb;EN-US;909455 it was an impersontion issue.

Then came the next issue

<exception>
System.Exception: Microsoft.SharePoint.SPException: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. ---&gt; System.Runtime.InteropServices.COMException (0x8102006D): The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
   at Microsoft.SharePoint.Library.SPRequestInternalClass.DeleteSite(String bstrUrl, Boolean bDeleteADAccounts)
   at Microsoft.SharePoint.Library.a.c(String A_0, Boolean A_1)
   --- End of inner exception stack trace ---
   at Microsoft.SharePoint.Library.a.c(String A_0, Boolean A_1)
   at Microsoft.SharePoint.SPSite.Delete()
   at Service.deleteSTSSite(String strSiteName)
   at Service.deleteSTSSite(String strSiteName)
   at Service.deleteSite(String strSiteName, String strUserName, String strPassword)

</exteption>

The Fix for this was to add site.AllowUnsafeUpdate = true.  The SPSite.AllowUnsafeUpdate and SPWeb.AllowUnSafeUpdate properties must be set to true if you are not able to do the call from a web browser (in my case it is getting called from another piece of automation).  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tsurlCAMLBatch_SV01032775.asp has a little info on this.

You will get the last exception if you try do anything like spsite.Quota = SPGlobalConfig.quotatemplates.item(<templateName>) or SPWeb.anonymousState = SPweb.WebAnonymousState.

Recommend to set it back to false after you make your change.

So my new code that does the impersonation and the AllowUnsafeUpdate is

For Site Creation

<code>
Private Function createSTSSite(ByVal strSiteName As String, ByVal strTitle As String, ByVal strDescription As String, ByVal strWebTemplate As String, _
ByVal strOwner As String, ByVal strOwnerName As String, ByVal strOwnerEmail As String) As Boolean
 
 dim identity As System.Security.Principal.WindowsImpersonationContext = System.Security.Principal.WindowsIdentity.GetCurrent.Impersonate
        Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
        Dim uri As New Uri(("http://" & strSiteName))
        Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
        Dim site As SPSite = VS.Sites.Add(uri.ToString, strTitle, strDescription, Convert.ToUInt32(1033), strWebTemplate, strOwner, strOwnerName,
strOwnerEmail)
        site.AllowUnsafeUpdates = True
        If setQuotatemplate(strSiteName, gv_strQuotaTemplate) <> True Then
            site.Delete()
            Return False
        End If
        If Me.Exists(strSiteName) Then
            If Me.setAnonymousSettings(strSiteName, SPWeb.WebAnonymousState.On) <> True Then
                deleteSTSSite(strSiteName)
                Return False
            End If
            site.AllowUnsafeUpdates = False
            Return True
        Else
            Return False
        End If

    End Function
</code>

For Site exist

<code>
Private Function Exists(ByVal strSiteName As String) As Boolean
        dim identity As System.Security.Principal.WindowsImpersonationContext = System.Security.Principal.WindowsIdentity.GetCurrent.Impersonate

        Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
        Dim uri As New Uri(("http://<SERVER NAME>"))
        Try
            Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
            Dim site As SPSite = VS.Sites.Item(("http://" & strSiteName))
            site.AllowUnsafeUpdates = True
            If (site.AllWebs.Count <> 0) Then
                Return True
            End If
        Catch ex As System.IO.FileNotFoundException
            Return False
        End Try
 
 return False

    End Function
</code>

For Site Deletion

<code>
 Private Function deleteSTSSite(ByVal strSiteName As String) As Boolean
        'check to see if it already exists
        If Not Me.Exists(strSiteName) Then
            Return True
        End If
 dim identity As System.Security.Principal.WindowsImpersonationContext = System.Security.Principal.WindowsIdentity.GetCurrent.Impersonate

        Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
        Dim uri As New Uri(("http://" & strSiteName))
        Try
            Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
            'Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = x.OpenVirtualServer(uri)
            Dim site As SPSite = VS.Sites.Item(("http://" & strSiteName))
            site.AllowUnsafeUpdates = True
            site.Delete()
        Catch ex As Exception
            Return False
        End Try
        'make sure it exists
        If Me.Exists(strSiteName) <> True Then
            Return True
        Else
            Return False
        End If
    End Function
</code>

For SetQuota

<code>
Private Function setQuotatemplate(ByVal strSiteName As String, ByVal strQuotaTemplate As String) As Boolean
        dim identity As System.Security.Principal.WindowsImpersonationContext = System.Security.Principal.WindowsIdentity.GetCurrent.Impersonate
        Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
        Dim uri As New Uri(("http://" & strSiteName))
        Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
        Dim site As SPSite = VS.Sites.Item(("http://" & strSiteName))
        Dim GlobalConf As Microsoft.SharePoint.Administration.SPGlobalConfig = GlobalADM.Config
        site.AllowUnsafeUpdates = True
        site.Quota = GlobalConf.QuotaTemplates.Item(strQuotaTemplate)
        site.AllowUnsafeUpdates = False
        Return True
    End Function
</code>

For Set AnonymousState
<code>
   Private Function setAnonymousSettings(ByVal strSiteName As String, ByVal enumAnonType As SPWeb.WebAnonymousState) As Boolean
        dim identity As System.Security.Principal.WindowsImpersonationContext = System.Security.Principal.WindowsIdentity.GetCurrent.Impersonate
        Dim GlobalADM As New Microsoft.SharePoint.Administration.SPGlobalAdmin
        Dim uri As New Uri(("http://" & strSiteName))
        Dim VS As Microsoft.SharePoint.Administration.SPVirtualServer = GlobalADM.OpenVirtualServer(uri)
        Dim GlobalConf As Microsoft.SharePoint.Administration.SPGlobalConfig = GlobalADM.Config
        Dim site As SPSite = VS.Sites.Item(("http://" & strSiteName))
        site.AllowUnsafeUpdates = True
        Dim SiteCollection As SPWebCollection = site.AllWebs

        For Each web As SPWeb In SiteCollection
            web.AllowUnsafeUpdates = True
            web.AnonymousState = enumAnonType
            web.AllowUnsafeUpdates = False
        Next
        site.AllowUnsafeUpdates = False
        Return True
    End Function
</code>


It is a huge pain in the ASS!