Romeo Pruno

Sharepoint: Create/Delete web sites programmatically

Vi giro un esempio circa l'aggiunta/eliminazione di siti SharePoint basati su dei templates. A volte capita di voler associare un template dinamicamente ad un nuovo sito, magari scegliendo proprio un "stp" nella template gallery del server MOSS.

Esempio#1 - Create SharePoint WebSite

1 Public Function CreatSite(ByVal siteName As String) As Integer 2 Dim ris As Integer 3 Dim SPSite As SPSite = Nothing 4 Dim SPWeb As SPWeb = Nothing 5 Dim SPCollection As SPWebCollection 6 Dim newWeb As SPWeb = Nothing 7 8 Try 9 SPSite = New SPSite("http://") 10 SPWeb = SPSite.OpenWeb() 11 SPCollection = SPWeb.Webs 12 'Aggiunta del nuovo sito web in base al LCID di installazione 13 SPWeb.AllowUnsafeUpdates = True 14 Select Case SPWeb.Language 15 Case "1033" 16 newWeb = SPCollection.Add("siteName", _ 17 "title","description",SPWeb.Language, "template.stp", True, False) 18 SPWeb.Update() 19 Case "1040" 20 newWeb = SPCollection.Add("siteName", _ 21 "title","description",SPWeb.Language, "template.stp", True, False) 22 SPWeb.Update() 23 End Select 24 'Controllo sulla riuscita dell'operazione 25 For Each SPWeb In SPCollection 26 If SPWeb.Name = siteName Then 27 ris = 1 28 Exit For 29 Else 30 ris = 0 31 End If 32 Next 33 Return ris 34 35 Catch ex As Exception 36 'Eccezione 37 Return -1000 38 39 Finally 40 If SPSite IsNot Nothing Then SPSite.Dispose() : SPSite = Nothing 41 If SPWeb IsNot Nothing Then SPWeb.Dispose() : SPWeb = Nothing 42 If newWeb IsNot Nothing Then newWeb.Dispose() : newWeb = Nothing 43 44 End Try 45 End Function

Esempio#2 - Delete SharePoint WebSite

1 Public Function DeleteSite(ByVal siteName As String) As Integer 2 Dim ris As Integer 3 Dim SPSite As SPSite = Nothing 4 Dim SPWeb As SPWeb = Nothing 5 Dim SPCollection As SPWebCollection 6 7 Try 8 SPSite = New SPSite("http://") 9 SPWeb = SPSite.OpenWeb() 10 SPCollection = SPWeb.Webs 11 'Eliminazione del website 12 SPWeb.AllowUnsafeUpdates = True 13 For Each SPWeb In SPCollection 14 If SPWeb.Name = siteName Then 15 SPWeb.Delete() 16 ris = 1 17 Exit For 18 Else 19 ris = 0 20 End If 21 Next 22 Return ris 23 Catch ex As Exception 24 'Eccezione 25 Return -1000 26 27 Finally 28 If SPSite IsNot Nothing Then SPSite.Dispose() : SPSite = Nothing 29 If SPWeb IsNot Nothing Then SPWeb.Dispose() : SPWeb = Nothing 30 31 32 End Try 33 End Function
Technorati Tags: , ,
Posted: ago 29 2007, 03.33 by romeo | with 1 comment(s) |
Filed under:

Comments

TrackBack said:

# agosto 30, 2007 3.31