SharePoint Development Blog

Nick Boumans
View my LinkedIn Profile Follow me on Twitter View my Profile on FaceBook View my projects on CodePlex View my presentations on SlideShare



Recent posts

Tags

Categories

Navigation

Pages

Archive

Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

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]

 

Posted: Nov 02 2008, 00:00 by Nick Boumans | Comments (1) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

SharePoint desinger workflow or Visual Studio workflow?

Microsoft developed SharePoint designer original for powerusers and not especially for developers. In SharePoint designer you could easily create a workflow and add them to e.g. a SharePoint library from within the tool. Pretty easy! A lot of developers see this a chance to easy develop a workflow in all situations and don't use VIsual Studio to write all that code. The problem exist by trying to put your solution to another environment (e.g. from development to staging). Since SP Designer directly connects a workflow to a list, it is not easy to deploy it to another environment. You can find some tricks on the internet for changing the id's in the XOML file. But we are professional developers, aren't we? So please think carefully about your decission using SharePoint designer or Visual Studio. Visual Studio gives you the ability to deploy your workflow by a feature. Use SharePoint designer for easy to configure workflows. Use Visual Studio for more complex workflows or solutions which you have to put on different environments.
Posted: Jul 21 2008, 09:40 by Nick Boumans | Comments (3) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Workflow

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]

 

Posted: Jul 19 2008, 00:00 by Nick Boumans | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Starting a Workflow from an Event Receiver

Out of the Box there are only a few options to start a (custom) workflow. E.g. when an item is created or changed. This means that there are a lot of situations that a workflow is fired where it is not needed (often a workflow only needs to be started if a field has a specific value). Therefore we can write an event receiver to time better when a workflow is started. Below you find the code to start a workflow from your event receiver.

[code:c#]
public override void ItemUpdated(SPItemEventProperties properties)  
{  
  if(YourCondition)  
  {  
    SPList parentList = properties.ListItem.ParentList;  
    SPWorkflowAssociation associationTemplate =         
      parentList.WorkflowAssociations.GetAssociationByName("Workflow Name",   
      new CultureInfo  
        (Convert.ToInt32(parentList.ParentWeb.RegionalSettings.LocaleId)));  
    SPSite siteCollection = properties.ListItem.ParentList.ParentWeb.Site;  
      siteCollection.WorkflowManager.StartWorkflow(properties.ListItem,  
      associationTemplate, String.Empty);  
  }  

[/code]

Posted: Apr 06 2008, 00:00 by Nick Boumans | Comments (4) RSS comment feed |
  • Currently 4.4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Workflow