GD Extensions

by Gunjan Datta

Introduction

I was sick and tired of copying common code from project to project, that I took a couple weekends and created this library. I wanted a way for developers to call these methods easily & without having to copy code. Instead of referencing methods, I made it available in the SP objects.

Create a discussion for any new methods/requests you want me to add.

How to use this library

To use this library, you simply reference the dll in your visual studio project. At the top of the code page, add:

using GDExtensions;

The extension methods will now be available in your code. Below is an example of how to add a folder to a list.

SPList list = SPContext.Current.Web.Lists.TryGetList("Name of your list");
list.CreateFolder("New Folder Name");

That's it. Below is a list of extension methods and the objects to find them in.

WSP - Add Library to Package

To ensure the solution deploys the library, below is a list of steps to follow. For those who are not allowed to deploy to the GAC, I'm sure you're experienced enough to figure out how to include 3rd party dlls.
  1. Double-Click the "Package" to open the package interface
  2. Click on "Advance"
  3. Click "Add" -> "Add Existing Assembly..."
  4. Select the source path
  5. Click on "OK"

List of Controls

Control Extension Methods:
  1. FindControlById - Method to find a control by a specified id. It will search the child controls and the parent's child controls before searching recursively through every control on the page.
  2. FindControlByIdRecursively - Method to search a control and it's children recursively for a specified id.
  3. FindFieldControls - Method to find all form fields and field labels on the list form.
  4. SetTemplateName - Method to set the form field's template name. This method is for use when customizing a list form page.
  5. UpdateFieldLabels - Method to update the form label's control mode property. This method is for use when customizing a list form page.
  6. UpdateReadOnlyFields - Method to set the read-only control mode property to "Display". This method is for use when customizing a list form page.

SPFolder Extension Methods:
  1. AddSecurityGroup - Method to add a security group to a folder.
  2. ClearPermissions - Method to clear the folder's permissions. By default it will add the owner's group.
  3. CreateSubFolder - Method to create a sub folder.
  4. RemoveSecurityGroup - Method to remove a security group from a folder.

SPGroup Extension Methods:
  1. None for now.....

SPItemEventProperties Extension Methods:
  1. GetField - Method to get a field by the internal or display name.
  2. GetFieldValue - Method to return the field value. There is an optional input parameter to only return the updated value (AfterProperties) value only. By default it will return the value being updated, if found in AfterProperties, or the list item's current value.
  3. IsUniqueValue - Method to query the list to see if the value inputted is unique.
  4. IsUpdateRequired - Method to determine if anything in the list item is being updated. It's valuable when versioning is turned on, to prevent a version from being created if it's not needed. An optional parameter is available to ignore the case when comparing strings.

SPList Extension Methods:
  1. AddSecurityGroup - Method to add a security group to a list.
  2. ClearData - Method to clear the list data. It uses the ProcessBatchData method to ensure it's completed quickly.
  3. ClearPermissions - Method to clear the list's permissions. By default it will add the owner's group.
  4. CreateFolder - Method to create a folder. If the url is not given, it will create it at the root.
  5. CreateMethodTemplate - Method to return a template for the ProcessBatchData method. It has an optional string array parameter for creating the template.
  6. DeleteListItemsById - Method to delete list items, given an array of item ids. This method uses the process batch data method to delete the list items.
  7. GetField - Method to get the field by display or internal name.
  8. GetItemByQuery - Method to query the list and return an item, if the CAML query provides a result.
  9. GetItemsByQuery - Method to query the list and return the items, if the CAML query provides results.
  10. GetNextAvailableItemId - Method to query the next available item id.
  11. RemoveSecurityGroup - Method to remove a security group from a list.

SPListItem Extension Methods:
  1. AddSecurityByUserId - Method to grant a user access to a list item.
  2. AddSecurityGroup - Method to add a security group to a list item.
  3. ClearPermissions - Method to clear the list item's permissions.
  4. FieldValueExists - Method to determine if a field value exists.
  5. GetField - Method to get a list field by display or internal name.
  6. GetFieldValue - Method to a field value from the list item.
  7. RemoveSecurityByUserId - Method to remove user access to a list item.
  8. RemoveSecurityGroup - Method to remove a security group to a list item.

SPSite Extension Methods:
  1. AddSecurityGroup - Method to add a security group to the root web.
  2. CreateSecurityGroup - Method to create a security group in a root web.
  3. CreatePermissionLevel - Method to create a custom permission level.
  4. RemoveSecurityGroup - Method to remove a security group from a root web.
  5. RunAsAdmin - Method to elevate a custom method and pass it an N number of input parameters. This method will require the InputParameters structure.

SPWeb Extension Methods:
  1. AddSecurityGroup - Method to add a security group to the web.
  2. InputParameters - Structure for the RunAsAdmin method.
  3. CreateSecurityGroup - Method to create a security group in a web.
  4. GetAssociatedSecurityGroup - Method to get an associated security group (Members, Owners or Visitors) from the web.
  5. GetRoleDefinitionByName - Method to return SPRoleDefinition object by the name of the role definition.
  6. GetSecurityGroup - Method to return the SPGroup object by the name of the security group.
  7. ProcessBatchMethods - Method to the list batch methods. Refer to the 'CreateMethodTemplate' extension for the SPList object.
  8. RemoveSecurityGroup - Method to remove a security group from a web.
  9. RemoveWebParts - Method to remove a list of web parts from the web part gallery. This is used in the web/site feature OnDeactivation method to clean up the custom web parts.
  10. RunAsAdmin - Method to elevate a custom method and pass it an N number of input parameters. This method will require the InputParameters structure.

Last edited Aug 7, 2012 at 5:19 PM by djgonzo81, version 14

Comments

No comments yet.