Pages

Friday, 2 September 2011

Prevent duplicate item in a SharePoint List

The following Event Handler code will prevent users from creating duplicate value in "Title" field.

ItemAdding Event Handler
 public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            if (properties.ListTitle.Equals("My List"))
            {
                try
                {
                    using(SPSite thisSite = new SPSite(properties.WebUrl))
                    {
                        SPWeb thisWeb = thisSite.OpenWeb();
                        SPList list = thisWeb.Lists[properties.ListId];
                        SPQuery query = new SPQuery();
                        query.Query = @"" + properties.AfterProperties["Title"] + "";
                        SPListItemCollection listItem = list.GetItems(query);
                        if (listItem.Count > 0)
                        {
                            properties.Cancel = true;
                            properties.ErrorMessage = "Item with this Name already exists. Please create a unique Name.";
                        }
                    }
                }
                catch (Exception ex)
                {
                    PortalLog.LogString("Error occured in event ItemAdding(SPItemEventProperties properties)() @ AAA.BBB.PreventDuplicateItem class. Exception Message:" + ex.Message.ToString());
                    throw new SPException("An error occured while processing the My List Feature. Please contact your Portal Administrator");
                }
            }
        }


Feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="1c2100ca-bad5-41f5-9707-7bf4edc08383"
          Title="Prevents Duplicate Item"
          Description="Prevents duplicate Name in the "My List" List"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

<font color="red">Element.xml</font>


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="100">
    <Receiver>
      <Name>AddingEventHandler</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>AAA.BBB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8003cf0cbff32406</Assembly>
      <Class>AAA.BBB.PreventDuplicateItem</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

Error: The search request was unable to connect to the Search Service

Scenario: Recieved an error in our SharePoint site when we try to search contents

Error message: "The search request was unable to connect to the Search Service"

Resolution:

1. Go to Central Admin > Operations > Services on Server > Office SharePoint Server Search Service
Make sure the "Use this server for serving search queries" is checked.

2. Also verify that the search account is configured in both search services
Go to Central Admin > Operations > Services on Server > Office SharePoint Server Search service
Go to Central Admin > Operations > Services on Server > Windows SharePoint Services Search

3. Make sure the search account specified in "Default content access account" is used on above settings.

SharePoint Useful CodePlex Tools for SharePoint


To manage where the column are displayed (e.g.: new or edit mode). To Hide columns from newform.aspx, editform.aspx and dispform.aspx etc
http://wsslistconfigurator.codeplex.com/

To add 'Only their own' permission for the document library.
http://moresharepoint.codeplex.com/

Ad Rotator WebPart
http://spadrotator.codeplex.com/

Caml Query Builder
http://spadrotator.codeplex.com/

JQuery SPServices
http://spservices.codeplex.com

Hit Counter WebPart
http://hitcounter.codeplex.com/

Cascading drop downs
http://customfieldcontrols.codeplex.com/

To manage alerts on list or item for SharePoint groups.
http://advancedalert.codeplex.com

Useful Sharepoint Designer Custom Workflow Activities
http://spdactivities.codeplex.com/wikipage?title=Copy%20List%20Item%20Extended%20Activity&ProjectName=spdactivities

SharePoint User Change Password
http://userchangepassword.codeplex.com/>

List of users and their access
http://accesschecker.codeplex.com/

SharePoint Navigation Menu
http://spnavigationmenu.codeplex.com/>

Popular Posts