Tuesday, June 14, 2011

Filter data in a DataView Web Part using SharePoint Designer


Follow the steps to filter the Overall Project Status by its Status, Assume that we need to list all the project that are in open status.

  1. Open the page that contains the Data View that you want to filter.
  2. Right-click the Data View, and then click Show Common Control Tasks on the shortcut menu.
  1. In the Common Data View Tasks list, click Filter.
  2. In the Filter Criteria dialog box, click Click here to add a new clause.
  3. Select more fields from Field Name drop downFilter in DataView WebPart
  4. Select the field Status field and Click Ok
  5. Enter the value as Open in the Value field and click OK
  6. Apply Filter in DataView WebPart
  7. Save the file and refresh the page to verify the webpart.

Thursday, June 2, 2011

Check if a folder exists in a SharePoint List


Below code is to check if the folder already exists in a SharePoint List.


 private void CreateFolderInList(string folderName, string listName, SPListCollection listCollection)
        {
            try
            {
                //Creating folder in "Sites" Lists
                SPList list = listCollection[listName];

                //Check if the Folder is already available in the list
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='Title'/><Value Type='Text'>" + folderName + "</Value></Eq><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></And></Where>";

  query.ViewAttributes = "Scope=\"RecursiveAll\""


                //Retrieve the items based on Query
                SPListItemCollection items = list.GetItems(query);

  //Item count is "0" if the folder does not exist
                if (items.Count == 0)
                {
                    folderItem = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
                    folderItem["Title"] = folderName;
                    folderItem.Update();
                    //return folderItem.Url;
                }
            }
            catch (Exception ex)
            {
               

            }
}

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 = @"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + properties.AfterProperties["Title"] + "</Value></Eq></Where>";
                        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>

Element.xml


<?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>

The source code of this feature is available at Technet Gallery