SharePoint In Action

An attempt to share my day-to-day SharePoint experience

Archive for the month “April, 2012”

Randy Drisgill’s Starter Master Pages for SharePoint

Hi,

Just wanted to share this cool link. It’s Randy’s SharePoint 2010 customized master page. A very good starting point for branding your SharePoint sites:

http://startermasterpages.codeplex.com/

Cheers,
Nader Heshmat

SharePoint 2010 Event Receivers – Modifying List Items

Hi,

In this post I will explain how to create and add an event receiver to your SharePoint site. This event receiver will then be attached to a content type in your site. This way all lists created based on that specific content type will use our event receiver once a new list item was added.

Here is what my event receiver is doing in a nutshell:

At the time of creating a new list item, it will create a Guid and assigns it to “LookupID” column of our content type “MyContentType”:

Title LookupId LookupName LookupValue LookupDescr LookupActive

Here is the screen-shot for “MyContentType”: 

MyContentType

MyContentType

 And here is the “Location” list driven from “MyContentType”:

 

In my event receiver I added the code below:

using System.Security.Permissions;
using System.Net;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace <strong>ER_InsertGuidToMyList</strong>
{
 
    public class <strong>ER_ContentTypeReceiver</strong> : SPItemEventReceiver
    {
       public override void <strong>ItemAdded</strong>(SPItemEventProperties properties)
       {
           SPListItem item = properties.ListItem;
           Guid newGuid = Guid.NewGuid();
           item["LookupId"] = newGuid.ToString();
           item.Update();
       }     

    } 
}

 And in my feature’s code behind, I modified the FeatureActivated method:

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Reflection;

namespace <strong>ER_InsertGuidToMyList</strong>.Features.Feature1
{
    [Guid("3c04bbf8-7cad-490b-935e-59d13bf91376")]
    public class <strong>Feature1EventReceiver</strong> : SPFeatureReceiver
    {
        public override void <strong>FeatureActivated</strong>(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                SPContentType BoundContentType = web.ContentTypes["MyContentType"];               

                if (BoundContentType == null)
                {
                    return;
                }
                BoundContentType.EventReceivers.RemoveEventReceivers(typeof(ER_ContentTypeReceiver), SPEventReceiverType.ItemAdded);
                BoundContentType.EventReceivers.AddEventReceivers(typeof(ER_ContentTypeReceiver), SPEventReceiverType.ItemAdded);

            catch (Exception err)
            {
                ....
            }
        }      
    }
}

and the last part is creating “Extensions” class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace <strong>ER_InsertGuidToMyList</strong>
{
    public static class  <strong>Extensions</strong>
    {             
        public static void <strong>AddEventReceivers</strong>(this SPEventReceiverDefinitionCollection collection,Type erClassType,
            params SPEventReceiverType[] erTypes)
        { 
            foreach (SPEventReceiverType erType in erTypes)  
            {     
                collection.Add(erType, erClassType.Assembly.FullName,
                    erClassType.FullName);  
            }}
       
      

        public static void <strong>RemoveEventReceivers</strong>(this SPEventReceiverDefinitionCollection collection, Type erClassType, params SPEventReceiverType[] erTypes)
        {
            List&lt;SPEventReceiverDefinition&gt; receivers = new List&lt;SPEventReceiverDefinition&gt;();
            for (int i = 0; i &lt; collection.Count; ++i)
            {
                SPEventReceiverDefinition r = collection[i];
                if (r.Class.Equals(erClassType.FullName) &amp;&amp; (erTypes.Length == 0 || erTypes.Contains(r.Type))) receivers.Add(r);
            }
           
            foreach (SPEventReceiverDefinition r in receivers) { r.Delete(); }
        }
    }
}

Once I deploy the event receiver and activate it, mycontenttype will have this receiver attached to and any list driven from this content type will fire “ItemAdded” method.

Cheers,

Nader Heshmat

Programmatically Creating SharePoint 2010 Wiki Pages Based On Your Own Wiki Templates

Hello all,

Last week I was asked to prepare a tool for our company intranet users to let them create new Wiki pages based on pre-made Wiki templates. Here is what I did in order to create the tool:

–          Created a Wiki page library to store my Wiki pages

–          Created a Wiki page library to store my Wiki page templates

–          Created a SharePoint 2010 sandboxed web part in VS 2010. The web part had these functionalities:

  1. Reading  the Wiki page template list and showing the name of templates in a drop down list
  2. Getting a name for the new Wiki page as well as the template name
  3. Creating the new Wiki page based on the template which was selected

Here is a screen shot for the web part:

Web Part - Customized Wiki Pages

Web Part – Customized Wiki Pages

and Here is the link to the code for our Web Part:

Web Part Code – Customized Wiki pages

Notes:

–          In CreateChildControls method, the web part’s interface is built

–          In PopulateTemplatesDropDown method we read the templates list and show the names in a dropdown list

–          In GetWikiTemplateContent method we get the content of selected template

–          In ChangeWikiContent method we swap the content of newly created wiki with template’s content

The most important trick is accessing the “WikiField” item of a Wiki page. It contains the content of the Wiki page. Once you have it, all you need to do is change it with whatever you like.

Hope this helped you create your own Wiki pages based on your customized Wiki templates.

Cheers,
Nader Heshmat

Hello Everyone!

Welcome to my corner!

Here, I will try to share whatever I think might be interesting for other SharePoint developers. Due to my day-to-day SharePoint development job, I should be able to update this blog regularly.

Please stay tuned for my first post!

Cheers,
Nader Heshmat

p.s.
You can also find me on LinkedIn: http://ca.linkedin.com/in/naderheshmat

View Nader Heshmat's profile on LinkedIn

Post Navigation