Interview: SharePoint 2010 from Visual Design standpoint

March 4th, 2010
Share this on Twitter

Today I had short but very interesting interview with Kurtis Beard, from Habanero Consulting. Kurtis is an Interaction Designer and stands behind some of the most stunning site designs. Kurtis has been involved in creating design for few SharePoint 2010 projects and has some valuable insights on what to look out for as well as some general strategies when designing for SharePoint 2010. The interview is about 13 min and you`ll find it very informative. Enjoy!

Interview with Kurtis: SharePoint 2010 Insights from Visual Designer from Yaroslav Pentsarskyy on Vimeo.

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 ,

Creating custom SharePoint 2010 Service Jobs

March 3rd, 2010
Share this on Twitter

As in MOSS, SharePoint 2010 has a notion of Service Jobs, running on a defined schedule.

Usually you’d use service job to create scheduled tasks within your portal such as batch business rules validation or alerts processing or really just about anything.

When you create your own Service Job it will be added to this list: http://[central admin url]/_admin/ServiceJobDefinitions.aspx

All Service Jobs have to be provisioned under web application. The best approach is to use SharePoint 2010 feature scoped to a Web Application and create an event receiver for the feature that will create an instance of a job based on the definition.

Here is how you’d go about creating a service job:

1. In your SharePoint solution create a new feature and scope it to the web application.

2. In the Template tree create a new folder where you will define any helpers that will be used to define your Service Job.

3. Create new feature receiver in the feature created in istep 1 and populate the following code in feature activated

SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
// remove any existing job definitions

foreach (SPJobDefinition jobDefinition in jobs)
            {
                if (jobDefinition.Name.StartsWith(”My Service Job Name“, StringComparison.InvariantCultureIgnoreCase))
                {
                    jobDefinition.Delete();
                }
            } 

            try
            {
                MyJobDefinition myJobDefinition = new MyJobDefinition(”"MyServiceJobId“”, webApplication);

            SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();
            minuteSchedule.BeginSecond = 1;
            minuteSchedule.EndSecond = 59;
            minuteSchedule.Interval = 1000;
            myJobDefinition.Schedule = minuteSchedule;
            myJobDefinition.Update();
            }
            catch (Exception ex)
            {
                Microsoft.Office.Server.Diagnostics.PortalLog.DebugLogString(
                    Microsoft.Office.Server.Diagnostics.PortalLogLevel.Unexpected,
                    “Exception in Feature Activated occurred: {0} || {1}”,
                    ex.Message, ex.StackTrace);
                throw;
            }

4. Your feature deactivating method will have Service Job cleanup that will look something like this:

SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
// remove any existing job definitions

foreach (SPJobDefinition jobDefinition in jobs)
            {
                if (jobDefinition.Name.StartsWith(”My Service Job Name“, StringComparison.InvariantCultureIgnoreCase))
                {
                    jobDefinition.Delete();
                }
            } 

5.  Now you noticed we have MyJobDefinition  class from which we create a new job. This class will define what your job is actually doing. It has one method called Execute that will execute the job, you can either call another class that performs your operations if those are complex or you can define operations right in the Execute, here is how something like this will look like:

public class MyJobDefinition : SPJobDefinition
    {
        #region Constructors
        /// <summary>
        /// Default constructor is needed.
        /// </summary>
        public MyJobDefinition()
            : base()
        {
        }
        /// <param name=”jobName”></param>
        /// <param name=”webApplication”></param>
        public MyJobDefinition(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.Job)
        {
            this.Title = “My Service Job Name“; //this will appear in Service Jobs list inside central admin
        }
        #endregion

        #region Methods

        /// <param name=”targetInstanceId”>ID of the content db that is processed by this job.</param>
        public override void Execute(Guid targetInstanceId)
        {
            try
            {

                SPWebApplication webApplication = this.Parent as SPWebApplication;
                // TODO: your scheduled code is here
            }
            catch (Exception ex)
            {
                Microsoft.Office.Server.Diagnostics.PortalLog.DebugLogString(
                    Microsoft.Office.Server.Diagnostics.PortalLogLevel.Unexpected,
                    “Exception in MyJobDefinition occurred: {0} || {1}”,
                    ex.Message, ex.StackTrace);
                throw;
            }
        }
        #endregion
    }

That’s it. Now this is basic implementation and you can improve various parts of your job such as pulling the schedule from configuration file rather than hardcoding it in the code etc. Good luck with your scheduled tasks!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , ,

Creating SharePoint 2010 permissions levels

March 2nd, 2010
Share this on Twitter

If you’re building custom solutions for SharePoint you probably have various types of users that are accessing the prtal with all sorts of permissions on different containers such as document libraries and lists etc. Quite often this results in you needing to create custom level of access to the group and define what funcitons users are allowed to access such as item creation, editing etc.

Here is how to assing all of those settings programatically so that you can call them from the feature receiver when creating a site from your site template:

1. First we create custom permission level with the name and description and set of permissions you can find here.

SPRoleDefinition role = new SPRoleDefinition();
                role.BasePermissions = SPBasePermissions.OpenItems | SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems | SPBasePermissions.ViewPages | SPBasePermissions.Open | SPBasePermissions.ViewFormPages;
                role.Name = “My Role Name”;
                role.Description = “My Role Description”;
                rootWeb.RoleDefinitions.Add(role);

Then we assign a set of permissions to an existing group in this case called MyGroup

                SPRoleAssignment roleAssignment = new SPRoleAssignment(rootWeb.SiteGroups["MyGroup"]);
                roleAssignment.RoleDefinitionBindings.Add(role);
                rootWeb.RoleAssignments.Add(roleAssignment);

If you’re reading this you probably spent about 10 minutes trying to find this particular code. Here is something I found very usefull if you’re developing for MOSS or SharePoint 2010: a great set of SharePoint snippents from CodePlex.

This isn’t just a one line of code type of snippets (well some are), but mostly those are really handy pieces of functionality and it’s really good to know it’s out there when you need it.

Cheers!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , , ,

Include SharePoint 2010 rating in search results webpart

March 1st, 2010
Share this on Twitter

If you remember last little while I was working on customizing SharePoint 2010 ratings control, well, it’s time to write some more about adding rating control to other areas o f the site, for example SharePoint 2010 Search enter, Search Results to be specific. It would be a nice touch to have documents and list items in search results along with teir corresponding ratings, after all if you enabled your users to use rating – they will get used to the feature and would like to see it in more places and Search is first on the list.

Here is how you’d go about something like that:

1. Naigate to your search page and in the edit mode of your page select Edit Webpart on the Search Core Results WP

search core reults webpart

2. Now in the DisplayProperties -> Fetched Properties add the following item to the list of fetched columns:

<Column Name=”Rating”/>

3. Cick XSL Editor button right below the Fetched Properties field. You will see quite a bit of XML in the following window so paste it to Notepad and find the following line of code:

<xsl:call-template name=”DisplayAuthors”>

and paste the following code right above it. This will make sure the rating control appears right before the author information in the search results:

          <xsl:choose>
            <xsl:when test=”rating > 0″>
              <span>
                <xsl:attribute name=”title”>
                  <xsl:value-of select=”rating”/>
                </xsl:attribute>
                <xsl:call-template name=”stars”>
                  <xsl:with-param name=”starCount” select=”rating”/>
                </xsl:call-template>
                <xsl:if test=”round(rating) > rating”>
                  <img src=”/_Layouts/Images/Ratings/RatingsNew.png”/>
                </xsl:if>
              </span>
              <br/>
            </xsl:when>
            <xsl:otherwise>
              <b>Not Rated</b>
              <br/>
            </xsl:otherwise>
          </xsl:choose>

4. Now scroll down the XML find the section where you see definitions of various templates, here is hwo we’re going to implement the template for the  rating, paste the following:

<xsl:template name=”stars”>
    <xsl:param name=”starCount”/>
    <xsl:param name=”value” select=”1″/>
    <xsl:if test=”$value &lt;= $starCount”>
      <img src=”/_Layouts/Images/Ratings/RatingsNew.png”/>
      <xsl:call-template name=”stars”>
        <xsl:with-param name=”starCount” select=”$starCount”/>
        <xsl:with-param name=”value” select=”$value + 1″/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

5. Lastly, we need to provision te Rating search preoperty (column we’re going to grab a data from). In your central admin Service Applications (http://[central admin URL]/_admin/ServiceApplications.aspx) highligh Search Service Administration  and click Manage on the ribbon.

6. Pefrom a full search crawl which you can initiate from Content Sources  left nav link. Wait for the crawl to complete.

6. Pick Metadata Properties from the left nav menu and click New Managed Property from the newly opened page.

7. For name of the property type Rating of ty Decimal , then click Add Mapping

8. In the new modal box search for rating  and pick column name that will be of the type of decimal.

9. Click OK to add new managed property.

That’s it. Now search results will include a image representing a rating for each search result and if some items have not been rated they will have “Not rated” instead.

Good luck!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , ,