Programmatically loop thru Publishing site collection or rather WARMP UP Script for FBA site
I wanted to write a WARMP UP SCRIPT For FBA enabled Publishing site for Sharepoint 2007/ MOSS.
REQUIREMENT:
We have a web-application set up with FBA and we have some of the sites accessible anonymously and other for only authenticated users.
We would like to programmatically loop thru the entire site-collection, I am somehow not able to loop thru the site collection for the authenticated sites and I get access denied.
I am sure I am missing some simple step on creating a generic principal with Forms based authentication but not sure where and how I make sure that I can loop thru the site collection.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System.Net;
using System.Web.Security;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//args[0]: This is the Site Collection url, e.g.: http://pub-staging.ABCD.com
//args[1]: This is web service URL for Authentication, e.g: http://pub-staging.ABCD.com/_vti_bin/Authentication.asmx
//args[2]: This is the username
//args[3]: This is the password
//string strSiteURL = "http://pub-staging.ABCD.com";
string strSiteURL = args[0];
System.Net.CookieContainer cookie;
bool blnStatus = FBAAuthenticate(out cookie,args[1],args[2],args[3]);
using (SPSite site = new SPSite(strSiteURL))
{
foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine("***********************************************************");
Console.WriteLine(web.Url.ToString());
Console.WriteLine("***********************************************************");
//FormsAuthenticationTicket fbaTkt = new FormsAuthenticationTicket("fsegment10", true, 10);
if (PublishingWeb.IsPublishingWeb(web))
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pageColl = publishingWeb.GetPublishingPages();
//Call the web service Authentication
foreach (SPListItem listItem in publishingWeb.PagesList.Items)
{
if (PublishingPage.IsPublishingPage(listItem))
{
PublishingPage page = PublishingPage.GetPublishingPage(listItem);
try
{
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(page.Uri.ToString());
req.CookieContainer = cookie;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
using (System.IO.Stream str = res.GetResponseStream())
{
using (System.IO.StreamReader strReader = new System.IO.StreamReader(str))
{
Console.WriteLine(strReader.ReadToEnd());
strReader.Dispose();
}
}
}
catch (Exception ex)
{
}
}
}
}
}
}
}
/// <summary>
/// Sharepoint forms based authentication (FBA) web method call.
/// </summary>
/// <param name="cookieContainer">CookieContainer</param>
/// <param name="strSiteId">String</param>
/// <returns>Boolean</returns>
private static bool FBAAuthenticate(out System.Net.CookieContainer cookieContainer, string webServiceURL, string userName, string pwd)
{
bool blnStatus;
FBAPORTAL.Authentication auth = new FBAPORTAL.Authentication();
//auth.Url = "http://pub-staging.ABCD.com/_vti_bin/Authentication.asmx";
auth.Url = webServiceURL;
auth.CookieContainer = new System.Net.CookieContainer();
auth.AllowAutoRedirect = true;
//FBAPORTAL.LoginResult loginResult = auth.Login("fbaadminstaging", "fba1234");
FBAPORTAL.LoginResult loginResult = auth.Login(userName,pwd );
if (loginResult.ErrorCode == FBAPORTAL.LoginErrorCode.NoError)
{
blnStatus = true;
cookieContainer = auth.CookieContainer;
}
else
{
blnStatus = false;
cookieContainer = null;
}
return blnStatus;
}
}
}