Sunday, November 16, 2008

SharePoint Site Provisioning (Code)

// FIRT TIME COMPLETE SITE PROVISIONING. YOU MIGHT NOT RUN THIS EVERY TIME
try
{
// JOB STARTING

SPWebApplication app = SPWebApplication.Lookup(new Uri(ConfigurationCollection.Instance.ApplicationPath));

List allowPrefixes = new List();
foreach (Configuration usersGroup in ConfigurationCollection.Instance.Configurations)
{
allowPrefixes.Add(usersGroup.SitesLocation);
}

List siteNames = new List();
WebApp.ProcessSites(true, app, delegate(SPSite site)
{
using (SPWeb root = site.RootWeb)
{
foreach (string prefix in allowPrefixes)
{
if (root.ServerRelativeUrl.TrimStart('/').StartsWith(prefix))
{
siteNames.Add(root.ServerRelativeUrl.TrimStart('/'));
root.Properties[WebApp.propPKIDiSDirty] = "true";
root.Properties.Update();
return;
}
}
}
});



foreach (Configuration configuration in ConfigurationCollection.Instance.Configurations)
{
Configuration configuration1 = configuration;
ProvisioningModule.ProcessConfig(configuration,
delegate(Dictionary userInfo)
{
string url = ProvisioningModule.GetEntityRelativeUrl(userInfo);
if (!siteNames.Contains(url))
{
using (SPSite site =
CreateSite(app, url, Formatter.GetFormatString(Convert.ToString(userInfo[ProvisioningModule.fieldEntityFormatField]), userInfo),
configuration1, userInfo))
using (SPWeb root = site.RootWeb)
{
root.Properties[WebApp.propPKID] =
userInfo[ProvisioningModule.fieldPKID].ToString
();
root.Properties.Update();
}
Log.Trace(Level.Info, "Created site{0}", url);
Console.WriteLine("Created site {0}", url);
}
else
{
SPSecurity.RunWithElevatedPrivileges(delegate {
using (SPSite site = new SPSite(WebApp.ApplicationPath + url))
using (SPWeb web = site.RootWeb)
{
web.Properties[WebApp.propPKIDiSDirty] = "false";
web.Properties.Update();
}
});
// UPDATED SITE

}
});
}

if (ConfigurationCollection.Instance.PerformArchival)
{
WebApp.ProcessSites(true, app,
delegate(SPSite site)
{
using (SPWeb root = site.RootWeb)
{
if (null != root.Properties[WebApp.propPKIDiSDirty] && root.Properties[WebApp.propPKIDiSDirty].Contains("true"))
{
if (!string.IsNullOrEmpty(ConfigurationCollection.Instance.ArchiveLocation))
ReloacateSite(app.Sites, site,
ConfigurationCollection.Instance.ApplicationPath + ConfigurationCollection.Instance.ArchiveLocation +
site.ServerRelativeUrl);
else
StripAccess(root);
}
}
}
);
}
// JOB FINISHING
}
catch (Exception e)
{
// JOB ABORTED
}

No comments: