SharePoint Dragons

Nikander & Margriet on SharePoint

Monthly Archives: June 2012

Starting and Stopping a service instance

If you want to start and stop services programmatically, you can use the Provision() method of an SPServiceInstance object to start it, and its Unprovision() method to stop it. You can find details about this at http://msdn.microsoft.com/en-us/library/ee537799.aspx

The following C# code demonstrates, using the server object model run from a console app, how to start and stop the Access service status. You can check the Services on Server page (in our case, http://astro:40000/_admin/Server.aspx ) to see what happens when you run the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SPServiceCollection services = SPFarm.Local.Services;
                foreach (SPService service in services)
                {
                    Console.WriteLine(“SPService {0} status: {1}”, service.Name, service.Status);

                    foreach (SPServiceApplication application in service.Applications)
                    {
                        Console.WriteLine(“SPServiceApplication {0} Status: {1}”, application.Name, application.Status);
                        if (application.Name.ToLower().Contains(“access”))
                        {                           
                            Console.WriteLine(“Toggling Access service instances”);
                            foreach (SPServiceInstance serviceInstance in application.ServiceInstances)
                            {
                                Console.WriteLine(“SPServiceInstance {0} Status: {1}”, serviceInstance.Name, serviceInstance.Status);
                                if (serviceInstance.Status == SPObjectStatus.Online)
                                {
                                    Console.WriteLine(“Stopping service instance”);
                                    serviceInstance.Unprovision();
                                }
                                else if (serviceInstance.Status == SPObjectStatus.Disabled)
                                {
                                    Console.WriteLine(“Starting service instance”);
                                    serviceInstance.Provision();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                var errMsg = err.Message;
            }
        }
    }
}

Microsoft Surface

Not really what we normally blog about, but the innovations at Microsoft and the width of their businesses continue to amaze us. We know that all of you already heard about it, but Surface looks really cool: http://www.microsoft.com/surface/en/us/gallery.aspx It looks like soon, we can throw our iPad away!

Requests per second –SharePoint Performance Management

It’s well written, entertaining, and very informational: http://www.cleverworkarounds.com/2012/06/09/demystifying-sharepoint-performance-management-part-4making-use-of-rps/ (check out the other parts as well). We liked it so much that we’ve added it to the SharePoint 2010 TN Wiki Best Practices page at http://social.technet.microsoft.com/wiki/contents/articles/8666.sharepoint-2010-best-practices-en-us.aspx. We couldn’t resist to mention our favorite quote:

To that end, I would also be seriously remiss if I did not make you aware of the SharePoint Flavored Weblog Reader tool. It was created by Nikander & Margriet Bruggeman who run the SharePoint Dragons blog – probably the best SharePoint performance related blog out there. This tool was specifically designed to make it easier to analyse IIS logs for SharePoint specific information. It is a command line tool, but much simpler and slicker than the methods I introduced in this post. Instead of specifying a date range you specify the number of items from the logs to process. For example:

sfwr.exe 250,000 “E:\LOGS\IIS_WWW\W3SVC1045333159”

SharePoint 2010 Workflows in Action

We’re frequently technically reviewing books and we thought it would be nice to devote some blog posts about a bunch of them. In this case, it’s about a topic close to us, SharePoint 2010 Workflows in Action: http://www.amazon.com/SharePoint-2010-Workflows-Action-Wicklund/dp/1935182714/ref=sr_1_1?ie=UTF8&qid=1338295214&sr=8-1 The book discusses:

  • Diagramming workflows,
  • Ootb wokrflows,
  • SharePoint Designer workflows,
  • Visio workflows,
  • Creating InfoPath and ASP.NET forms,
  • VS.NET workflows,
  • Custom activities and conditions,
  • Fault handling, versioning, and debugging tips.

Setting an alert on a view

In SharePoint 2010 you can set alerts on many things, like lists, libraries, folders, document sets, items. But did you know that you can also set an alert on a view?

The most important thing to know about setting an alert on a view is that the view must be configured with a filter otherwise the option will not appear in the alert settings.

  1. First you have to select the view which you want the alert for
  2. Select an item (that could be a folder, document set or item) and click Alert Me
  3. On the Alert Me page, go to the Send Alerts for These Changes section and select the Someone changes an item that appears in the following view radio button.

    image

Retrieve folder using client object model that has space in its name

You have to be a little bit careful when you’re retrieving a SharePoint folder name using the client object model that has a space in its name. The correct way to do it is to replace the space with “%20”:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Client;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //string folderName = “/MyDox/Test”;

                string folderName = “/MyDox/Name%20withSpace”;

                ClientContext context = new ClientContext(“http://astro“);

                Web web = context.Web;

                List list = context.Web.Lists.GetByTitle(“MyDox”);

Folder folder = web.GetFolderByServerRelativeUrl(folderName);               

context.Load(context.Web);

                context.Load(list);

                context.Load(folder);               

                context.ExecuteQuery();

                Console.WriteLine(folder.Name);

            }

            catch (Exception err)

            {

                Console.WriteLine(err.Message);

            }

        }

    }

}

How to change the Created By column

Sometimes you don’t want a column, for example the Created By column, to be a link to user info but just plain text.  You can do this very easy with SharePoint Designer:

  1. First of all you have to open your SharePoint list in SharePoint Designer
  2. In the Views section, select the view that you want to modify

    image

  3. Go to the Design view of the selected view
  4. Click on any name displayed in the Created By column
  5. Go to the List View Tools in the ribbon and select the Design tab
  6. Click Customize XSLT in the Actions section and select Customize Item

    image

  7. Check if the name you selected earlier (Step 4) is still selected and click Delete.

    image

  8. Go to the Current Date Source and select Author.title

    image

  9. Drag Author.title to the place where you deleted the name from (Step 7)

Save the view in SharePoint Designer en refresh your SharePoint site to take a look at the result.

Note: This change is only made for this particular view of a list. If you would have another view, that view would still use the Created By column with a hyperlink.

Referencing the SharePoint DLL

Using Visual Studio 2010 and SharePoint 2010 you will notice that the SharePoint DLLs are not listed in the Add Reference > NET tab.

image

Follow the next steps to add a SharePoint DL reference:

  1. In Visual Studio 2010, right click your project and select Properties
  2. Go to the Application tab and set the Target framework to 3.5
  3. Go to the Build tab and set the Platform to x64
  4. Go to your project in the Solution Explorer and select Add Reference.
  5. Browse to the ISAPI folder in the SharePoint root to add the SharePoint DLL.

But, instead of these 5 steps above you could also install CKS Development Tools Edition (http://cksdev.codeplex.com/) to make this a lot easier. You can install the CKS Development Tools via Tools > Extension Manager. After installing you will notice a new SharePoint tab in Add Reference which makes it very easy to add a reference to the SharePoint DLL.

image

Forms authentication using the Client Object Model

A question that is asked regularly is how to do forms authentication when you’re using the SharePoint client object model. We have blogged about this before at https://sharepointdragons.com/2012/04/20/authentication-when-using-the-sharepoint-client-object-model/ , but the solution below is clean and well worth to mention (it uses the managed client object model):

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {               
                using (var context = new ClientContext(“http://astro:46454″))
                {
                    context.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
                    FormsAuthenticationLoginInfo formsAuthInfo = new FormsAuthenticationLoginInfo(“anton”, “anton!0”);
                    context.FormsAuthenticationLoginInfo = formsAuthInfo;

                   
                    var site = context.Site;
                    context.Load(site);
                    context.ExecuteQuery();
                    Console.WriteLine(site.Url);
                }
            }
            catch (Exception err)
            {
                var errMsg = err.Message;
            }
        }
    }
}

Application Pages: Auto Activate to Central Admin

Application pages are a special type of ASP.NET pages. An application page is designed to be used in a SharePoint site and uses a SharePoint master page. Using a master page you ensure that the application pages have the same look and feel as the other SharePoint pages. It is also possible to use an application page in SharePoint Central Administration.

The following MSDN article is a guideline for building application pages for SharePoint Central Administration: http://msdn.microsoft.com/en-us/library/ff798467.aspx.

Let’s take a look at the following Note:

image

There is another way to make sure that your feature is activated on the Central Administration Web site, just follow the next steps:

  1. Go to your Visual Studio project.
  2. In Solution Explorer, expand Features and expand your custom feature.
  3. Double click the .feature file.
  4. Take a look at the properties and set the Auto Activate in Central Admin property to True

    image

  5. Press F5 to deploy and test your solution

Note: You cannot set the local site to the URL of your Central Administration Web site and the Auto Activate in Central Admin property to True. You have to choose which solution you want to use.