185 lines
6.8 KiB
C#
185 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Principal;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using Microsoft.SharePoint.WebControls;
|
|
using Taloyhtio.CondoAutomation.Utils;
|
|
|
|
namespace CondoAutomation.CondoCreator
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length != 2)
|
|
{
|
|
log("Usage: .exe <site_col_url> <file_with_condos> (file lines structure: <condo_name>;<condo_short_name>;<condo_url>;<business_id>)");
|
|
return;
|
|
}
|
|
|
|
string siteColUrl = args[0];
|
|
string filePath = args[1];
|
|
|
|
|
|
var parser = new Parser();
|
|
var info = parser.Parse(filePath);
|
|
if (info == null || info.Count == 0)
|
|
{
|
|
log("Parsed info is empty");
|
|
return;
|
|
}
|
|
|
|
|
|
foreach (var creationInfo in info)
|
|
{
|
|
string condoName = creationInfo.Name;
|
|
string condoShortName = creationInfo.ShortName;
|
|
string condoUrl = creationInfo.Url;
|
|
string businessId = creationInfo.BusinessId;
|
|
|
|
if (!validateUrl(siteColUrl, condoUrl))
|
|
{
|
|
log("Condo url is invalid");
|
|
return;
|
|
}
|
|
|
|
using (var site = new SPSite(siteColUrl))
|
|
{
|
|
if (!validateNameIsUnique(site, condoName))
|
|
{
|
|
log("Condo name is already used");
|
|
return;
|
|
}
|
|
|
|
if (!validateShortNameIsUnique(site, condoShortName))
|
|
{
|
|
log("Condo short name is already used");
|
|
return;
|
|
}
|
|
|
|
var rootWeb = site.RootWeb;
|
|
|
|
// AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
|
|
// HttpContext.Current = new HttpContext(new HttpRequest("", rootWeb.Url, ""), new HttpResponse(TextWriter.Null));
|
|
// HttpContext.Current.Items["HttpHandlerSPWeb"] = rootWeb;
|
|
// HttpContext.Current.User = Thread.CurrentPrincipal as WindowsPrincipal;
|
|
|
|
try
|
|
{
|
|
log(
|
|
"Creating condo: site col '{0}', condo name '{1}', condo short name '{2}', condo url '{3}', business id '{4}'",
|
|
siteColUrl, condoName, condoShortName, condoUrl, businessId);
|
|
string webTemplate = null;
|
|
using (var web = rootWeb.Webs.Add(
|
|
//strFullWebUrl,
|
|
condoUrl,
|
|
condoName,
|
|
"", // description
|
|
1035,
|
|
webTemplate,
|
|
true, // unique permissions
|
|
false))
|
|
{
|
|
WebPropertyHelper.Set(web, Constants.PropertyBag.KEY_CONDO_SHORT_NAME, condoShortName);
|
|
|
|
log("Applying web template...");
|
|
web.ApplyWebTemplate("TaloyhtioCondoSitePortal#0");
|
|
|
|
log("Post configuration...");
|
|
string url = SPUrlUtility.CombineUrl(web.Url, string.Format("_layouts/15/Taloyhtio/PostConfigurationHandler.ashx?{0}={1}",
|
|
Constants.QueryString.BUSINESS_ID, HttpUtility.UrlEncode(businessId)));
|
|
var request = (HttpWebRequest) WebRequest.Create(url);
|
|
request.Credentials = CredentialCache.DefaultNetworkCredentials;
|
|
var response = (HttpWebResponse) request.GetResponse();
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
log(
|
|
"Error occured during sending request to post configuration handler: response code is '{0}'",
|
|
response.StatusCode);
|
|
}
|
|
else
|
|
{
|
|
log("Condo site was successfully created");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
log("Error occured: {0}\n{1}", x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static bool validateUrl(string siteUrl, string condoUrl)
|
|
{
|
|
try
|
|
{
|
|
string strFullWebUrl = SPUrlUtility.CombineUrl(siteUrl, condoUrl);
|
|
var uri = new Uri(strFullWebUrl);
|
|
ReflectionHelper.CallStaticMethod(typeof (SPWebCollection), "ValidateWebUrl", uri);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
protected static bool validateNameIsUnique(SPSite site, string condoName)
|
|
{
|
|
return (!isWebExists(site, w => string.Compare(w.Title, condoName, true) == 0));
|
|
}
|
|
|
|
protected static bool validateShortNameIsUnique(SPSite site, string condoShortName)
|
|
{
|
|
return (!isWebExists(site, w => string.Compare(WebPropertyHelper.Get(w, Constants.PropertyBag.KEY_CONDO_SHORT_NAME) as string, condoShortName, true) == 0));
|
|
}
|
|
|
|
private static bool isWebExists(SPSite site, Func<SPWeb, bool> checker)
|
|
{
|
|
if (checker(site.RootWeb))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return site.AllWebs.Cast<SPWeb>().Any(w => isWebExists(w, checker));
|
|
}
|
|
|
|
private static bool isWebExists(SPWeb web, Func<SPWeb, bool> checker)
|
|
{
|
|
if (checker(web))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return web.Webs.Cast<SPWeb>().Any(w => isWebExists(w, checker));
|
|
}
|
|
|
|
private static void log(string msg, params object[] args)
|
|
{
|
|
try
|
|
{
|
|
if (args.Length > 0)
|
|
{
|
|
msg = string.Format(msg, args);
|
|
}
|
|
msg = string.Format("{0} (thread id: {1})\t{2}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId, msg);
|
|
Console.WriteLine(msg);
|
|
File.AppendAllText("log_condoCreator.txt", msg);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|