Create a custom Site Actions Menu Item Feature
You can create a Custom Action Menu using the next feature code:
Elements.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Feature
Id="Your GUID"
Title="Custom Action Menu"
Description="Shows a custom action menu"
Scope="Site"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
Feature.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="ApplicationPage1"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="2000"
Title="Hello World Application Page"
Description="Getting up and going with inline code" >
<UrlAction Url="http://www.google.com%22/>
</CustomAction>
</Elements>
You can read more about this item on MSDN: http://msdn.microsoft.com/en-us/library/bb418728.aspx
Creating a Publishing page from code
In some cases it could be necessary to create a page from code. Because
the publishing page is the hardest way to create from code (checkin /
approval) the next code example will help you completing this job.
Notice that I skipped complete error handling.
/// <summary>
/// Create a Publishing page based on an excisting Page Layout
/// Remember that a SPSite is a top-level site (collection) and
/// a SPWeb is the current site
/// </summary>
/// <param name="site">Current Site</param>
/// <param name="pageName">Name of the new page to create including .aspx</param>
/// <param name="pageLayoutName">Name of the Page Layout</param>
private void CreatePublishingPage(SPWeb site, string pageName, string pageLayoutName)
{
PublishingSite pubSiteCollection = new PublishingSite(site.Site);
PublishingWeb pubSite = null;
if (pubSiteCollection != null)
{
// Assign an object to the pubSite variable
if (PublishingWeb.IsPublishingWeb(site))
{
pubSite = PublishingWeb.GetPublishingWeb(site);
}
}
// Search for the page layout for creating the new page
PageLayout currentPageLayout = FindPageLayout(pubSiteCollection, pageLayoutName);
// Check or the Page Layout could be found in the collection
// if not (== null, return because the page has to be based on
// an excisting Page Layout
if (currentPageLayout == null)
{
return;
}
PublishingPageCollection pages = pubSite.GetPublishingPages();
PublishingPage newPage = pages.Add(pageName, currentPageLayout);
newPage.Description = pageName.Replace(".aspx", "");
// Here you can set some properties like:
newPage.IncludeInCurrentNavigation = true;
newPage.IncludeInGlobalNavigation = true;
// End of setting properties
SPFile publishFile = newPage.ListItem.File;
publishFile.Update();
newPage.Update();
// Check the file in (a major version)
publishFile.CheckIn("Initial", SPCheckinType.MajorCheckIn);
publishFile.Publish("Initial");
// In case of content approval, approve the file
if (pubSite.PagesList.EnableModeration)
{
publishFile.Approve("Initial");
}
}
/// <summary>
/// Find a page layout in the layoutcollection based on the templatename
/// </summary>
/// <param name="pubSiteCollection">The toplevel publishing site</param>
/// <param name="templateName">Name of the pagelayout</param>
/// <returns>Pagelayout if excists or null</returns>
private PageLayout FindPageLayout(PublishingSite pubSiteCollection, string templateName)
{
PageLayoutCollection plCollection = pubSiteCollection.GetPageLayouts(true);
foreach (Microsoft.SharePoint.Publishing.PageLayout layout in plCollection)
{
// String Comparison based on the Page Layout Name
if (layout.Name.Equals(templateName, StringComparison.InvariantCultureIgnoreCase))
{
return layout;
}
}
return null;
}
[/code]
Setting a custom masterpage and css using a feature without invalid cast exception
A lot of people have problems casting the SPSite to SPWeb, they are trying to do something like: SPWeb CurrentWeb = (SPWeb)properties.Feature.Parent. By trying this you get a invalid cast exception. Some people are trying to do it in one step and get a nullreference to the object. In the next lines you can find the right way to set a custom masterpage and css using a feature.
[code:c#]
// Don't forget to inherit your class from SPFeatureReceiver!
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPSite CurrentSite = properties.Feature.Parent as SPSite)
{
if (CurrentSite != null)
{
SPWeb CurrentWeb = CurrentSite.RootWeb;
CurrentWeb.MasterUrl = "/_catalogs/masterpage/custom.master";
CurrentWeb.CustomMasterUrl = "/_catalogs/masterpage/custom.master";
//In a publishing site the css is saved in the Style Library
CurrentWeb.AlternateCssUrl = "/Style Library/custom.css";
CurrentWeb.Update();
}
}
}
// Set it back to default
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPSite CurrentSite = properties.Feature.Parent as SPSite)
{
if (CurrentSite != null)
{
SPWeb CurrentWeb = CurrentSite.RootWeb;
CurrentWeb.MasterUrl = "/_catalogs/masterpage/default.master";
CurrentWeb.CustomMasterUrl = "/_catalogs/masterpage/default.master";
CurrentWeb.AlternateCssUrl = "";
CurrentWeb.Update();
}
}
}
[/code]
Adding a List - ListFields and Items from code
Often it is required to insert a list, listitem and listitems from code. An
example is creating a list if it not exists from a Feature or WebPart. By
creating a list we out of the box get the Title field. The next code shows the
process of creating a custom list, columns and items from code. It also shows
how you can change to out-of-the-box Title column.
// Option 1) Inside SharePoint
SPSite siteCollection = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
// Option 2) From an external tool
SPSite siteCollection = new SPSite("http://myurl");
SPWeb site = siteCollection.RootWeb;
// Get a ListCollection from the current site
SPListCollection lists = site.Lists;
// Add a custom List to the SharePoint site
lists.Add("Employee", "List containing employees", SPListTemplateType.GenericList);
// Put the new list in a variable
SPList employeeList = lists["Employee"];
// Get the out of the box fields (Title, Attachment etc)
SPFieldCollection fields = employeeList.Fields;
// Change the default title column
SPField field = fields["Title"];
field.Title = "Name";
// Don't forget the update to commit changes
field.Update();
// Add a new field
SPField fieldAdd = fields[fields.Add("Address", SPFieldType.Text, true)];
SPView view = employeeList.DefaultView;
SPViewFieldCollection viewFields = view.ViewFields;
// Add the field to the defaultview (all items)
viewFields.Add(fieldAdd);
view.Update();
// Add a record to the new list
SPListItem item = employeeList.Items.Add();
item["Name"] = "Name of the Employee";
item["Address"] = "Address of the employee";
item.Update();
[/code]
Apply custom masterpage globally by a Feature
Becky Bertram wrote a nice post about applying a custom masterpage globally by a Feature. The full explanation and code could be found on her blog. The functions for Feature activating and deactivating are well written.
Read Becky's Post