Wednesday, July 4, 2012

Create pages programatically in SharePoint 2010


Hello,
- In this post we will get specified the content types in the sites and using that content type we will create the page programatically
- Using window application we will do this stuff.
- So first create the window application and add the Microsoft.Sharepoint.dll reference from the 14 hives directory.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\isapi )
-Now put some control as shown below and also give the appropriate name already mention below.





Now add the following lines of code insite the button click event and also add the custon methods.


//This function will use to get the specified content type and add those to the combo box
private void btnContentType_Click(object sender, EventArgs e)
{
using (SPSite site = new SPSite(txtURL.Text))
{
using (SPWeb web = site.OpenWeb())
{
PublishingSite pSite = new PublishingSite(site);
SPContentType ctype = pSite.ContentTypes[comboBoxContentType.SelectedText];
foreach (PageLayout p in pSite.PageLayouts)
{
if (p.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 1".Trim().ToLower() && p.Name.ToLower() == "CustomPageLayouts1.aspx".ToLower())
{
comboBoxContentType.Items.Add(p.AssociatedContentType.ResourceFolder.Name);
}
else if (p.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 2".Trim().ToLower() && p.Name.ToLower() == "CustomPageLayouts2.aspx".ToLower())
{
comboBoxContentType.Items.Add(p.AssociatedContentType.ResourceFolder.Name);
}
}
}
}
}
//Add the following line of code to the selected index change event of the combo box
private void comboBoxContentType_SelectedIndexChanged(object sender, EventArgs e)
{
if (pageLayoutForCreatingThePage != null)
{
lblPageLayouts.Text = pageLayoutForCreatingThePage.Name;
}
}
//Add following line of code to the button click event of the Create button , this code will useful to create the pages
private void btnCreate_Click(object sender, EventArgs e)
{
try
{
using (SPSite site = new SPSite(txtURL.Text))
{
using (SPWeb web = site.OpenWeb())
{
PublishingSite pSite = new PublishingSite(site);
PageLayout pageLayout = null;
foreach (PageLayout p in pSite.PageLayouts)
{
if (comboBoxContentType.SelectedItem.ToString().ToLower() == "Custom content type 1".Trim().ToLower() && p.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 1".Trim().ToLower() && p.Name.ToLower() == "CustomPageLayouts1.aspx".ToLower())
{
pageLayout = p;
break;
}
else if (comboBoxContentType.SelectedItem.ToString().ToLower() == "Custom content type 2".Trim().ToLower() && p.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 2".Trim().ToLower() && p.Name.ToLower() == "CustomPageLayouts2.aspx".ToLower())
{
pageLayout = p;
break;
}
}
pageLayoutForCreatingThePage = pageLayout;
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
SPList list = pPages.PubWeb.PagesList;
SPQuery query = null;
string pageName = string.Empty;
string strQuery = string.Empty;
Hashtable hs = new Hashtable();


strQuery = @"<FieldRef Name='Title' />";
strQuery = string.Format(strQuery, pageName);
query = new SPQuery();
query.Query = strQuery;
if (list.GetItems(query).Count > 0)
{
DataTable dt = list.GetItems(query).GetDataTable();
foreach (DataRow row in dt.Rows)
{
hs.Add(row["Title"].ToString().ToLower(), row["Title"].ToString().ToLower());
}
}
for (int i = 0; i < Convert.ToInt32(textBoxPagesToCreate.Text); i++)
{


pageName = comboBoxContentType.SelectedItem.ToString().Trim().Replace(" ", "-") + "-" + System.DateTime.Now.AddDays(i + 1).AddHours(i + 1).ToShortDateString().Replace("/", "_").ToString();
if (!hs.ContainsKey(pageName.ToLower()))
{
string filename = comboBoxContentType.SelectedItem.ToString().Trim().Replace(" ", "-") + "-" + System.DateTime.Now.AddDays(i + 1).AddHours(i + 1).ToShortDateString().Replace("/", "_").ToString() + ".aspx";
PublishingPage pPage = pPages.Add(filename, pageLayout);
SPListItem newpage = pPage.ListItem;
//Here we are setting the "Title" field property of the page
newpage["Title"] = comboBoxContentType.SelectedItem.ToString().Trim().Replace(" ", "-") + "-" + System.DateTime.Now.AddDays(i + 1).AddHours(i + 1).ToShortDateString().Replace("/", "_").ToString();
if (pageLayout.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 1".Trim().ToLower())
{
//Here we are setting the "PublishStartDate" field property of the page
newpage["PublishStartDate"] = System.DateTime.Now.AddDays(i + 1).AddHours(i + 1);
}
else if (pageLayout.AssociatedContentType.ResourceFolder.Name.ToLower() == "Custom content type 2".Trim().ToLower())
{
//Here we are setting the "ReleasePublihshedDate" field property of the page
newpage["ReleasePublihshedDate"] = System.DateTime.Now.AddDays(i + 1).AddHours(i + 1);
}
newpage.Update();
newpage.File.CheckIn("Checked in");
newpage.File.Publish("published");
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}


MessageBox.Show("Operation Completed......");
}


1. Once the code is added , run the window application ,
2. First add the Site Url of you Site , second click on get content type button ( This will fetch the specified content type form the site)
3. Second select the desired content type from the combo box and insert the number of amount of page to create.
4. Third click on the Create button.
5. So it will create the number of pages that you specified using the selected content type.


Regards
Hiren Patel

No comments:

Post a Comment