Skip to content

Instantly share code, notes, and snippets.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
if (Utils.IsStringPresentInHtmlSource(pageContent, "Working on it"))
{
Console.WriteLine("Site Collection " + url + " has been deleted");
}
else
{
MSOWebPartPage_PostbackSource=&MSOTlPn_SelectedWpId=&MSOTlPn_View=0&MSOTlPn_ShowSettings=False&MSOGallery_SelectedLibrary=&MSOGallery_FilterString=&MSOTlPn_Button=none&MSOSPWebPartManager_DisplayModeName=Browse&MSOSPWebPartManager_ExitingDesignMode=false&__EVENTTARGET=ctl00$PlaceHolderMain$ctl07$RptControls$BtnDelete&__EVENTARGUMENT=&MSOWebPartPage_Shared=&MSOLayout_LayoutChanges=&MSOLayout_InDesignMode=&MSOSPWebPartManager_OldDisplayModeName=Browse&MSOSPWebPartManager_StartWebPartEditingName=false&MSOSPWebPartManager_EndWebPartEditing=false&_maintainWorkspaceScrollPosition=0
string postdata = "MSOWebPartPage_PostbackSource=&MSOTlPn_SelectedWpId=&MSOTlPn_View=0&MSOTlPn_ShowSettings=False&MSOGallery_SelectedLibrary=&MSOGallery_FilterString=&MSOTlPn_Button=none&MSOSPWebPartManager_DisplayModeName=Browse&MSOSPWebPartManager_ExitingDesignMode=false&__EVENTTARGET=ctl00$PlaceHolderMain$ctl07$RptControls$BtnDelete&__EVENTARGUMENT=&MSOWebPartPage_Shared=&MSOLayout_LayoutChanges=&MSOLayout_InDesignMode=&MSOSPWebPartManager_OldDisplayModeName=Browse&MSOSPWebPartManager_StartWebPartEditingName=false&MSOSPWebPartManager_EndWebPartEditing=false&_maintainWorkspaceScrollPosition=0&__REQUESTDIGEST=" + formDigest + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation + "&__VIEWSTATEGENERATOR=" + viewStateGenerator + "&SideBySideToken=" + sideBySideToken;
byte[] buffer = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = buffer.Length;
request.KeepAlive = true;
sw = request.GetRequestStream();
sw.Write(buffer, 0, buffer.Length);
sw.Flush();
sw.Close();
string htmlResult = GetHtmlSource("delete page url", credentials, request.CookieContainer);
string formDigest = ExtractHiddenValuesFromHtmlSource(htmlResult, "__REQUESTDIGEST");
string viewState = ExtractHiddenValuesFromHtmlSource(htmlResult,"__VIEWSTATE");
string eventValidation = ExtractHiddenValuesFromHtmlSource(htmlResult, "__EVENTVALIDATION"); ;
string viewStateGenerator = ExtractHiddenValuesFromHtmlSource(htmlResult, "__VIEWSTATEGENERATOR");
string sideBySideToken = ExtractHiddenValuesFromHtmlSource(htmlResult, "SideBySideToken");
public static string ExtractHiddenValuesFromHtmlSource(string htmlSource, string hiddenControlName)
{
string requestControlDelimiter = hiddenControlName;
string valueDelimiter = "value=\"";
int requestControlPosition = htmlSource.IndexOf(requestControlDelimiter);
int requestControlValuePosition = htmlSource.IndexOf(
valueDelimiter, requestControlPosition
);
public static string GetHtmlSource(string siteUrl, ICredentials credentials, CookieContainer cc)
{
string resourceUrl = siteUrl;
HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
wreq.Credentials = credentials;
wreq.CookieContainer = cc;
wreq.Method = "POST";
wreq.UserAgent = HtmlControlConstants.USERAGENT;
wreq.ContentLength = 0;
wreq.ContentType = HtmlControlConstants.CONTENTTYPE;
SharePointOnlineCredentials credentials = GetO365Credentials(userName, password);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = credentials;
request.CookieContainer = new CookieContainer();
request.CookieContainer = GetO365CookieContainer(credentials, url);
request.Method = "POST";
request.Headers.Add("Overwrite", "T");
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; BRI/2; InfoPath.3)";
public static CookieContainer GetO365CookieContainer(SharePointOnlineCredentials credentials, string targetSiteUrl)
{
Uri targetSite = new Uri(targetSiteUrl);
string cookieString = credentials.GetAuthenticationCookie(targetSite);
CookieContainer container = new CookieContainer();
string trimmedCookie = cookieString.TrimStart("SPOIDCRL=".ToCharArray());
container.Add(new Cookie("SPOIDCRL", trimmedCookie, string.Empty, targetSite.Authority));
return container;
}
public static SharePointOnlineCredentials GetO365Credentials(string userName, string passWord)
{
SecureString securePassWord = new SecureString();
foreach (char c in passWord.ToCharArray())
{
securePassWord.AppendChar(c);
}
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, securePassWord);
return credentials;
}
<script type="text/javascript" src="/teams/MySCForApps/Style Library/JS/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/teams/MySCForApps/Style Library/JS/jquery-ui.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/teams/MySCForApps/Style Library/JS/jquery.MultiFile.js"></script>
<link rel="Stylesheet" type="text/css" href="../CSS/App.css" />
<script language="javascript" type="text/jscript">
$(function () {
var fileCountCheck = 0;
$("#btnSubmit").click(function () {