Wednesday, January 19, 2011

Programmatically hide pages from Navigation

Suppose you are creating a page using Features, now you want that page to be hidden from the navigation when you create it. How will you go about it---

Create a Feature Receiver for that page and enter the below code at Feature activated event.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
PublishingWeb publishingWeb = null;
var objWeb = properties.Feature.Parent as SPWeb;
try
{
if (PublishingWeb.IsPublishingWeb(objWeb))
{
publishingWeb = PublishingWeb.GetPublishingWeb(objWeb);
// get the pages collection
PublishingPageCollection pages = publishingWeb.GetPublishingPages();

if (pages[publishingWeb.Url + "/Pages/chat.aspx"] != null)
{
//page exists hide it
PublishingPage page = pages[publishingWeb.Url + "/Pages/chat.aspx"];
page.IncludeInCurrentNavigation = false;
page.IncludeInGlobalNavigation = false;
}
}
}
catch (Exception ex)
{
using (StreamWriter w = new StreamWriter("C:\\log.txt"))
{
w.WriteLine(ex.ToString());
}
}
}

Wednesday, January 12, 2011

How to add a page in pages directory of Sharepoint site

Suppose you want to add a new page in the pages library of your sharepoint site programatically, how will you go about it.

Create the structure in your project -

12 - template - features - NewPageFeature -

In that create an aspx page, a feature.xml file and an elements.xml file

You will also need a cs file outside of the 12 folder, and will provide the path to the same in your aspx file.

Feature.xml ---------------------------------------------------------


Feature Id="94D273A9-B9AE-46f0-A56A-03289ACC87FF"
Title="IMCHATPAGE"
Description="Activate Chat Page"
Scope="Web"
Hidden="True"
xmlns="http://schemas.microsoft.com/sharepoint/"
ElementManifests
ElementManifest Location="Elements.xml"/
ElementManifests
/Feature


Element.xml -----------------------------------------------------------

?xml version="1.0" encoding="utf-8"? Module Name="Pages" Url="$Resources:cmscore,List_Pages_UrlName;" Path="" RootWebOnly="False"
File Name="Chat.aspx" Url="Chat.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="False" /
/Module
/Elements


.aspx file ---------------------------------------------------------

In your aspx file, include the path of you cs file like below -

Inherits="Eversheds.InstantMessaging.Chat, Eversheds.InstantMessaging,Version=1.0.0.0,Culture=neutral, PublicKeyToken=b0c7276684f0a390"

Run wsp builder and your page will be created in the pages directory of your site