Thursday 12 April 2012

How to Apply Unique value Constraint in Person and Group column in Sharepoint 2010

Hi,
Recently an issue was reported to me where one of the site admin was unable to apply uniqueness in a column of type Person or Group in a list. When he was trying to create a column of that kind he was getting a screen like this:











In the above screenshot there is no possibility to enforce uniqueness. Hence the next approach was to do this pro grammatically. In case of Sharepoint 2007 this would have required creating an event receiver or workflow that could have checked uniqueness . However in case of Sharepoint 2010 a property "EnforceUniqueValues" on SPField that can be marked as true and it will work out. However we need to be sure that there should not be any existing values in the list violating this constraint or else this will fail. We also need to be sure that this should get applied only on indexed column. Hence I have ensured this in the code itself.

Here is the code for this:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

namespace EnforceUniqueness

{

class Program

{

static void Main(string[] args)

{

try

{

Console.WriteLine("This tool will apply unique value constratint to a column in a list. Make sure that your list does not contain duplicate values in this column");

Console.WriteLine("\n");

Console.WriteLine("Enter the URL of Your Site");

String siteUrl = Console.ReadLine();

Console.WriteLine("Enter the Name of List name");

String listName = Console.ReadLine();

Console.WriteLine("Enter the Column Name");

String columnName = Console.ReadLine();

using (SPSite site = new SPSite(siteUrl))

{

SPWeb web = site.OpenWeb();

SPList list = web.Lists[listName];

SPField field = list.Fields[columnName];

field.Indexed = true;

field.EnforceUniqueValues = true;

field.Update();

Console.WriteLine("Tool Executed successfully. Press Enter to Terminate");

Console.ReadLine();

}

}

catch (Exception e)

{

Console.WriteLine(e.Message + "\n"+ e.StackTrace);

Console.ReadLine();

}

}

}

}

The execution will follow in this way:














I hope this will help you out.

Thanks,
Rahul Rashu

Sunday 8 April 2012

A Content Type Which Does Not Belong To A Collection Cannot be Updated In Sharepoint

Hi,

An issue was reported to me where a content type was not getting created programmatically. Whenever through code columns were added to it it was throwing the exception "A Content Type Which Does Not Belong To A Collection Cannot be Updated".  I did a walk through of the code and found that through code first a new content type was instantiated and then some columns were added to this. Here is the place where the exception was originating.

I referred some articles in MSDN and found out the reason for it. The reason was that although the content type was instantiated it was not added to any collection or in other words it was not associated with any site.
Hence we added following lines of code before calling update method and it became successful.

SPWeb web = site.OpenWeb();
web.ContentTypes.Add(MyContentType);

I hope this will help you out.

Thanks,
Rahul Rashu



Saturday 7 April 2012

Not Available locally Error while Executing Configuration Wizard in Sharepoint 2010


Hi,

Recently an issue was reported to me where execution of sharepoint configuration wizard was failing for addition of a server in an existing farm with the error message "Not Available locally". This was failing just at the step where the  connection to existing configuration database is carried out. The logged in user had all necessary rights including them in admin and content database. After some initial investigation I though of starting the wizard as an administrator option by right clicking on it and selecting "Run As Administrator". We did it and thats it it worked.

I hope this will help you out.

Thanks,
Rahul Rashu

Wednesday 4 April 2012

How to copy users between sharepoint groups in Sharepoint using C#

Hi,
One of the pain areas in sharepoint is to copy the users between the groups. The sharepoint does not support nesting of groups. The problem get worsen when you have huge number of users. To resolve this I have written this tool. This works in both versions of sharepoint MOSS 2007 and SPS 2010. This tool takes three input vales
1. Site Url
2. Name of the source sharepoint group (Case Sensitive).
3. Name of the destination sharepoint group (Case Sensitive).

The code for this tool is as below:

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

namespace CopyUsersBetweenGroupsInSharepointByRR
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool will copy the users from one group to another group");
            Console.WriteLine("Please enter the URL of the site where your groups are available");
            String siteUrl = Console.ReadLine();
            using (SPSite site = new SPSite(siteUrl))
            {
                try
                {
                    SPWeb web = site.OpenWeb();
                    Console.WriteLine("Please enter the name of the source group");
                    String sourceGroupName = Console.ReadLine();
                    Console.WriteLine("Please enter the name of the destination group");
                    String destinationGroupName = Console.ReadLine();
                    SPGroup sourceGroup = web.Groups[sourceGroupName];
                    SPGroup destinationGroup = web.Groups[destinationGroupName];
                    SPUserCollection sourceUsers = sourceGroup.Users;
                    SPUserInfo[] sourceUserInfoArray = new SPUserInfo[sourceUsers.Count];
                    for (int i = 0; i < sourceUsers.Count; i++)
                    {
                        sourceUserInfoArray[i] = new SPUserInfo();
                        sourceUserInfoArray[i].LoginName = sourceUsers[i].LoginName;
                        sourceUserInfoArray[i].Name = sourceUsers[i].Name;
                    }
                    destinationGroup.Users.AddCollection(sourceUserInfoArray);
                    destinationGroup.Update();
                    web.Update();
                    Console.WriteLine("Operation Completed Successfully");
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}

I hope this will help you out.

Thanks,
Rahul Rashu