Category Archives: CodePlex

SPDisposeCheck Static Code Analysis RuleSet for Visual Studio 2012

Do you remember the SPDisposeCheck Static Analysis RuleSet for Visual Studio 2008/10 ? It was pretty nifty, I’ve used it in many SharePoint 2010 projects and it did a great job. Yesterday, I decided to upgrade it to work with Visual Studio 2012 and I’ve published it today on CodePlex. You can get it from here.

This wonderful RulSset has been originally created by Stephen Vick, I just tweaked it a little bit to make it work with Visual Studio 2012.

What is Visual Studio Static Code Analysis ? http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx

What is SPDisposeCheck ? http://archive.msdn.microsoft.com/SPDisposeCheck

By using SPDisposeCheck from within your Code Analysis, you can avoid any future memory leaks that might occur because your developers didn’t properly dispose the SharePoint API objects. You can even prevent bad code from entering your Source Control system by creating a check-in policy that performs Static Code Analysis and spot any issues earlier.

I have created a video that explains the setup steps and shows you a quick example. You can watch it here.

4

 

5

Advertisement

Now on CodePlex: SPListConfiguration Feature

SPListConfiguration is a SharePoint feature that developers can use to configure their SharePoint lists using XML and without the need of writing .NET code.

SPListConfiguration can be used for many different purposes. For example, you can use it to set most of theproperties for the 5SPList object that references your list or to stop inheriting permissions from the parent web. You can even use it to add properties to the property bag of the list root folder.

For configuring a list using SPListConfiguration, you should specify the list name in the preoperty Key and the desired configuration settings in the property value as shown below :

<Properties>
<Property Key=”Calendar” Value=”Hidden,true;EnableVersioning,false;AddToPropertyBag,MyKey:MyValue”/>
<Property Key=”Tasks” Value=”BreakRoleInheritance,false;ReadSecurity,2″/>
</Properties>

The previous XML does the following on your behalf :
SPList.Hidden = true;
SPList.EnableVersioning= false;
SPList.BreakRoleInheritance(false);
SPList.ReadSecurity = 2;
And it also adds a property of name “MyKey” and value “MyValue” to the property bag of the SPlist root folder. Neat, eh ? This is the Reflection magic:)

SPListConfiguration can be used in two ways:

1) From a site definition; in the <WebFeatures> element.

<WebFeatures>
<Feature ID=”9697591b-c325-43e8-bf2f-3c33e05c59b4″>
<Properties>
<Property Key=”Calendar” Value=”Hidden,true;EnableVersioning,false;AddToPropertyBag,MyKey:MyValue”/>
<Property Key=”Tasks” Value=”BreakRoleInheritance,false;ReadSecurity,2″/>
</Properties>
</Feature>
</WebFeatures>

2) Since the key component of this feature is its feature receiver which performs all the heavy lifting,
you can only use the feature receiver assembly and hook it up as a receiver for your features :

<Feature Id=”ID of Your Feature” Title=”Countries List”
Description=”A list that contains all the countries of the world”
Hidden=”FALSE” Scope=”Web”
ReceiverAssembly=”SPListConfigurationFeature, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=064ae22bb48e8f1b”
ReceiverClass=”SPListConfigurationFeature.SPListConfigurationFeature”
xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<ElementManifests>
<!– The countries List is created here –>
<ElementManifest Location=”elements.xml”/>
</ElementManifests>
<Properties>
<!– The countries List is configured here –>
<Property Key=”Countries” Value=”Hidden,true;BreakRoleInheritance,false”/>
</Properties>
</Feature>

Also keep in mind that you set any list properties of type String, Boolean or Int32 and this covers almost 95% of the available properties.

Let me know what you think !

Announcing SPCountries

Have you ever found yourself in a situation where you need to create a SharePoint list that contains the names of all the countries of the world ? For me, I have faced this many times especially when developing public facing sites where I need to create forms and I need the form submitters to specify their countries of residence  along with other information.  The solution was very boring and time consuming, that is why I decided to share with you SPCountries which is a very simple feature that once activated, it creates a SharePoint list containing the names of all the countries of the world. Hope this helps 🙂

36

Get SPCountries from CodePlex : http://spcountries.codeplex.com

SharePoint Logging Library

In my article “Simplifying SharePoint Debugging By Creating the Troubleshooting Toolbox“, I have stated the following :

“I’d also like to point out that you can write code that incorporates logging to MOSS logs. Writing to the same trace log alleviates the need for developers to log their development information in other places such as the Windows Event Log, which is more commonly used by system administrators.”

 

Unfortunately this comes with 3 gotchas :

  1. You cannot set the log event level (e.g. low, medium, and critical) as it’ll always display the error level in the trace logs as High.
  2. Also you cannot control the trace category as it’ll always display the category as “General”.
  3. The logger is located in 12\ISAPI \Microsoft.Office.Server.dll and therefore it’s only available with the MOSS install, not WSS 3.0.

What if you need to write into SharePoint trace logs while having full control on the error level and category?

Enter SharePoint Logging Library !

  1. Get the “SharePoint Logging Library” from CodePlex.

 

    21

  1. Add a reference to AymanElHattab.SharePoint.Logging.dll .
  2. Add a using statement to AymanElHattab.SharePoint.Logging namespace.
  3. Use the library as follows :

    SharePointLogger.LogStartOfMethod();
    SharePointLogger.LogWarning(“Hello SharePointers”);
    SharePointLogger.LogInformation(“x = ” + x);
    SharePointLogger.LogMessageWithSeverityAndCategory(“Hi”, SharePointLogger.TraceSeverity.High, “My Category”);
    SharePointLogger.LogEndOfMethod();

  4. Get your code executed , then open the trace logs.

22

 As you might have noticed :

  1. You can call the public methods SharePointLogger.LogStartOfMethod() and SharePointLogger.LogEndOfMethod() for writing to SharePoint Trace Logs .You don’t need to pass the method or class name, this will be automatically logged by Reflection as shown in the figure above : “Beginning of NameSpace.ClassName.MethodName Method”.
  2. You can control the severity and the category when you use the LogMessageWithSeverityAndCategory() Method.
  3. “General” is the default category when you use the methods that doesn’t accept a parameter for the category.
  4. You can log exceptions and their stack traces by passing the exception object to the public LogException() Method.

Announcing SPCodeSnippets

Didn’t you find yourself writing SPContext.Current.Site, SPSecurity.RunWithElevatedPrivileges and list.Items.Add() more than hundred times in most of your SharePoint projects ? So Why Reinvent the wheel ?

3

4

SPCodeSnippets provides the most commonly used SharePoint code snippets that follow SharePoint development best practices.

Just integrate them into your visual studio , reuse the code and speed up your SharePoint development.

SharePoint List Association Manager

AW Systems has just released an open source tool called SLAM (SharePoint List Association Manager). Check it out now on CodePlex at http://www.codeplex.com/SLAM.

SLAM lets us do just that by allowing developers to describe complex relationships between lists and content types in a way not previously possible in SharePoint (at least not without a great deal of time and effort). With SLAM, these associations are managed automatically. Developers can then concentrate on building web parts and Custom Field Types that draw on those relationships using familiar and straightforward SQL queries.