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

Sunday, 25 March 2012

How to edit a list item by clicking on its title in Sharepoint 2010


Hi,

I received a request from someone to modify a list view in such a manner that if some click on the title of the item it should take him to the edit page instead of display page in sharepoint 2010. I have already managed to do this in sharepoint 2007 and have written an article on it here http://rahulrashu.blogspot.in/2012/03/how-to-edit-list-item-by-clicking-on.html . After some work with sharepoint designer 2010 I was able to mange it in few steps here as well . Here are the steps needs to be followed in this case:
1. Open the site in sharepoint designer.
2. Go to All Files.
3. Navigate down to the list where you want to do the modification.
4. Then open the view in sharepoint designer by double clicking on it in my case it was allitems.aspx.(Check it out first)
5. Now right click on the title part as shown in the screenshot below and select format hyperlink to Edit Form.















6. Save this file and you are ready to go.

I hope this will help you out.

Thanks,
Rahul Rashu

Saturday, 24 March 2012

How to edit a list item by clicking on its title in Sharepoint 2007

Hi,

I received a request from someone to modify a list view in such a manner that if some click on the title of the item it should take him to the edit page instead of display page. After some work with sharepoint designer I was able to mange it in few steps. Here are the steps needs to be followed in this case:
1. Open the site in sharepoint designer.
2. Navigate down to the list where you want to do the modification.
3. Then open the view in sharepoint designer by double clicking on it.
4. Now right click on the area of list viewer webpart and click the option to Convert to XSLT data view.
5. Now click on the section of title as shown





6. This will also select the portion in code part. There you will get a code portion as :
href="{$URL_Display}?ID={@ID}" onclick="GoToPage('{$URL_Display}?ID={@ID}')

7. Now just change it to:
href="{$URL_Edit}?ID={@ID}" onclick="GoToPage('{$URL_Edit}?ID={@ID}')
8. Save the page and you are ready to go.
9. Now if you click on title you will be taken to Edit form instead of dispform .

I hope this will help you out.

Thanks,
Rahul Rashu

Saturday, 3 March 2012

Your personal site cannot be created because the managed path "Path" has not been created for this site. Contact your site administrator for more information in Sharepoint 2010

Hi,

Recently one farm administrator reported a problem to me that they are unable to setup the mysites in SPS 2010 environment. They were following all the instructions in technet articles http://technet.microsoft.com/en-us/library/ee624362.aspx but still they were getting this error "Your personal site cannot be created because the managed path "Path" has not been created for this site. Contact your site administrator for more information". I suggested them to check if the managed paths are created and set up properly or not. They checked the same and found them OK. I suggested to create a new wildcard managed path and to test the same and they did so but their was nothing that could have caused this. At last I found the reason for it.

Under mysite settings their is field "Location". Here you need to add your managed path you have created for your mysites but you should only add its name and not with the full URL. So if you have created it as "Personal" it should be "Personal" only and not with the URL format as http://webApplication/mangedpath.

I asked them to change it and it worked.
I hope this will help you out.

Thanks,
Rahul Rashu