Code Standards, Practices, and Utilities for SharePoint

 

Two years ago I added a blog post on “MOSS Solution and Code Naming Conventions”. This subject comes up from time to time and, as a “quick reference”, I am posting some refinements and updates. The original is here: https://scottcurrier.wordpress.com/2009/03/01/moss-solution-and-code-naming-conventions/

Updated Links Next,

Life Cycle and Methods

Application Lifecycle Management (General): http://msdn.microsoft.com/en-us/library/gg604045.aspx

Application Lifecycle Management (Visual Studio): http://msdn.microsoft.com/en-us/vstudio/ff625779

Developing Applications for SharePoint 2010: http://msdn.microsoft.com/en-us/library/ff770300.aspx

Sandboxed Solution Considerations: http://msdn.microsoft.com/en-us/library/ee231562.aspx

Code Acceptance: http://technet.microsoft.com/en-us/library/cc707802(office.12).aspx

SharePoint Development Center: http://msdn.microsoft.com/en-us/sharepoint/default.aspx

Office 365

Office 365 Developers Guide: http://msdn.microsoft.com/en-us/library/hh147180.aspx

Utilities and Tools

SP Dispose Check (memory leaks): http://archive.msdn.microsoft.com/SPDisposeCheck

MSOCAF: https://caf.sharepoint.microsoftonline.com/Default.aspx

StyleCop (style and consistency rules): http://stylecop.codeplex.com/releases/view/64494

FxCop: See http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=8279 

For FxCop, as of this writing you will need to manually extract out of the SDK .ISO file if you don’t want the full SDK installed. see http://www.alexatnet.com/content/how-install-fxcop-10-0 

Team Foundation Server: http://msdn.microsoft.com/en-us/vstudio/ff637362

Roger Lamb’s Blog: http://blogs.msdn.com/b/rogerla/

ULS Viewer: http://archive.msdn.microsoft.com/ULSViewer <== added 5/16/2012

SPDiag 3.0: http://technet.microsoft.com/en-us/library/hh144782.aspx

Performance

Performance Counters: http://technet.microsoft.com/en-us/library/ff758658.aspx  <== added  1/18/2012

Quicker Downloads: http://technet.microsoft.com/en-us/library/hh206324.aspx

Web Performance Testing: Visual Studio Ultimate 2010 + SP1, see http://msdn.microsoft.com/en-us/library/aa337591.aspx

Web Testing Videos: http://www.bing.com/videos/search?q=Visual+Studio+2010+Load+Test+Wizard&mid=4320C2158B7365AF11B94320C2158B7365AF11B9&view=detail&FORM=VIRE3

lodctr.exe http://technet.microsoft.com/en-us/library/bb490926.aspx <== added 5/16/2012 (even helped fix User Code Host Service issues with SharePoint, using lodctr /r)

Web Performance Test TIP: if you are running on a local computer testing a remote machine on a different domain then run this command to be sure your remote performance counters map to the correct credentials

runas /netonly /user:domain\user "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" 

General Testing: http://msdn.microsoft.com/en-us/library/bb385901.aspx

Results and Recommendations: http://www.microsoft.com/download/en/details.aspx?displayLang=en&id=12768

Configure Usage Reporting: http://technet.microsoft.com/en-us/library/cc262541(office.12).aspx

Diagnostic Logging: http://technet.microsoft.com/en-us/library/ee748606.aspx

Conventions

http://msdn.microsoft.com/en-us/library/aa291596(VS.71).aspx

http://10rem.net/articles/net-naming-conventions-and-programming-standards—best-practices

http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx

Summary

Use your judgment ! You can’t do it all….be thoughtful, not draconian

Cheers!

PS: on a different topic – check out XAPTIC and SPDragon, my Silverlight and HTML5 widget library for SharePoint 2010 http://www.youtube.com/user/scottcurrier

On Building a Custom Service Application for SharePoint 2010

 

Added Part 9

I’ve begun a series of posts on building a custom Service Application for SharePoint 2010. For the most part I believe I have the process understood pretty well but I’m hoping if you see errors along the way you will feel free to point them out. As such, they should be considered a work in progress. I want these to ultimately be very comprehensive and reasonably detailed so I can use them as a reference myself.

For this series of posts I’m using the EMC public blogs. You can find the first two posts at the links below

Part 1: http://consultingblogs.emc.com/scottcurrier/archive/2011/08/16/on-building-a-custom-service-application-for-sharepoint-2010-part-1-background-and-decisions.aspx

Part 2: http://consultingblogs.emc.com/scottcurrier/archive/2011/09/12/on-building-a-custom-service-application-for-sharepoint-2010-part-2-service-application-hierarchy-and-sample.aspx

Part 3: http://consultingblogs.emc.com/scottcurrier/archive/2011/09/17/on-building-a-custom-service-application-for-sharepoint-2010-part-3-some-refactoring-and-administrative-tooling.aspx

Part 4: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/01/on-building-a-custom-service-application-for-sharepoint-2010-part-4-adding-administrative-code.aspx

Part 5: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/01/on-building-a-custom-service-application-for-sharepoint-2010-part-5-adding-administrative-code.aspx

Part 6: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/02/on-building-a-custom-service-application-for-sharepoint-2010-part-6-adding-business-logic-and-client.aspx

Part 7: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/02/on-building-a-custom-service-application-for-sharepoint-2010-part-7-better-engineering-with-proxy-classes.aspx

Part 8: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/04/on-building-a-custom-service-application-for-sharepoint-2010-part-8-client-access-via-proxy-classes.aspx

Part 9: http://consultingblogs.emc.com/scottcurrier/archive/2012/01/06/on-building-a-custom-service-application-for-sharepoint-2010-part-9-provisioning-a-service-application-database.aspx

Cheers!

Scott

PS: check out my Silverlight project called XAPTIC, it’s a widget and tile library for SP 2010 and Office 365 at http://www.youtube.com/user/scottcurrier

A Silverlight Web Part Framework for SharePoint 2010

 

This is part 5 of my updates regarding XAPTIC, my Silverlight Web Part Framework for SharePoint 2010. This weekend I focused on just a couple of needed updates.

  • Added property editors for plain and rich text
  • Added a rich text web part
  • Updated the the XAPTIC API to include property type values that are added when creating custom properties

Next week I focus on fleshing out the rich text editor more as well as start adding in some basic communication between web parts (even communication between XAPTIC HTML and Silverlight web parts).

Finally, I hope to have some time for a full blown JQuery/SPServices demo as well…we shall see Smile

Here is the video update of my latest efforts.

Thanks for watching!

Scott

XAPTIC: Connectivity and Fluid Elements

 

This is my fourth installment and update for XAPTIC, my weekend project to construct a robust Silverlight web part style framework for SharePoint 2010. This weekend was about bug fixing and presentation. Here is what I completed this weekend.

  1. Updated the Icons a bit – I’m not a UI guy and its difficult to make things look good
  2. Added some fluid interface elements (fader effects, etc.) – this piece is still a work in progress
  3. Added the client object model to one of my sample web parts (line chart)
  4. Added a CSS field to the HTML web part (soon JavaScript so I can dig into JQuery/SPServices more)
  5. Updated all the Silverlight parts for better appearance and presentation
  6. Added an Upload and Delete to/from the parts catalog (drag/drop coming soon)

I have so much technical information coming out of this project that I can hardly wait to blog some about it. For now I’ll post these updates as a sign of my progress.

One thing I did not say in the video update was that I actually created a VS 2010 project template. Now all you need to do is simply compile the default project and a fully operational “Hello World” style Silverlight Part is ready to use. See image below the video. I’ll blog that very soon

 

 

Here is the Xaptic Project Template

 

image  

 

Thanks for watching!

Scott

XAPTIC is now in SharePoint 2010

 

I am really excited that XAPTIC has progressed this far so soon – its been a great weekend project. I’ll have more to say in the coming weeks as well. The XAPTIC runtime will be free but its not ready for prime time just yet. The XAPTIC service application must provision the database during deployment and I also have some special handling for JQuery in the site page left to go. All in all I’ve never had more fun with a project.

See my previous XAPTIC posts at these links if your interested

https://scottcurrier.wordpress.com/2011/06/25/silverlight-parts-framework-for-sharepoint-2010/

https://scottcurrier.wordpress.com/2011/06/25/silverlight-parts-frameworkthe-xapticapi/ 

Here is a video update (XAPTIC IS IN SHAREPOINT NOW!)

 

Thanks for watching

Scott

XAPTIC: POST Complex Objects from Silverlight

 

In some of my previous posts I discussed a “beefy” project called XAPTIC which is a pure Silverlight web part framework. It’s a complex enough project to keep me interested and learning. Originally conceived as Silverlight “living” within an asp.net web application. This year I decided to move it into SharePoint 2010 as a full fledged environment with high end capability to construct SharePoint applications easily. It also makes great “source material” for my blog posts as well.

See, https://scottcurrier.wordpress.com/2011/06/25/silverlight-parts-framework-for-sharepoint-2010/

https://scottcurrier.wordpress.com/2011/06/25/silverlight-parts-frameworkthe-xapticapi/

The roadmap for migrating XAPTIC to SharePoint 2010 includes creating a XAPTIC service application and exposing its functionality via custom SharePoint REST based services. I can report that the service application is nearly done and deploys properly to SharePoint but that will be the subject for later posts.

For this post I’ll discuss using REST to POST objects (including potentially complex objects) to SharePoint 2010. The information presented here is somewhat preliminary and not “production” ready as yet but serves me well during this phase of development and my understanding.

Assuming you’re sending data back to SharePoint as a REST style service you would need to be concerned with several things – remember, unlike SOAP, you have no proxy – its all hand rolled (at least using the basic OTB classes). Here are some considerations

  • How to represent your class and instance data
  • Maintaining a well formed request (fiddler helps here)
  • Code to properly create that REST style request
  • For Silverlight, you need to keep everything asynchronous

For XAPTIC, I know the classes and parameters I need and therefore I can make highly specific decisions that keep my code rather simple.

For a little more background on building REST based services in SharePoint 2010 see the following links

https://scottcurrier.wordpress.com/2011/06/28/return-listmyobject-using-rest-from-sharepoint-2010/

That being said, I’ll cut to the bottom line. In my experimenting I found the best way to represent your class is by simply convert to an XElement. See

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=VS.90).aspx

I created a simple “converter” method to do just that (see code) ConvertUserFieldsToXElement(object) and used it as part of a method that POSTs back to SharePoint 2010. I won’t go into the details of the Converter method because its basic. More likely, if you’re understanding this post you would have little trouble designing one for a class of your own.

The following is the entire method used to POST back the data

image

You see I also pass two parameters called internalID and pageTitle as well. By the way, hard coding the URL is not recommended but good enough for a blog post. I felt converting my objects to XElement was by far the most flexible method of class representation and very easy to understand. I originally just serialized my object as text but had some difficulty with white space and related artifacts of the serialization in the body of the request message (but that’s your choice – it all works the same).

Here is the actual fiddler trace

image

This method is taken directly from XAPTIC’s Silverlight client – it’s job is to pass back data to SharePoint’s XAPTIC database and update parameters from a users web part placed within the canvas. It does this by first using the HttpWebRequest class forming a proper HTTP request but then adds fields (a LIST<> of field) to the body of the request. The internalID and pagetitle parameters are passed only as part of the URL. Note that I am using anonymous methods for both the request and response handling – that piece really simplifies the code.

The interface on SharePoint looks like this:

image

Of course, this is early code. I’m sure feedback could include comments about using WebClient and possibly more contemporary tools to accomplish the same goal – let me know

That’s it – it works well and brings me a step closer to XAPTIC in SharePoint 2010

Thanks – Scott

Return List<myobject> using REST from SharePoint 2010

 

I’m having a good time playing with WCF tonight. This is a quick post on the setup for returning lists of objects defined in a WCF REST service from within SharePoint 2010 – a common scenario. For related information see the following posts

https://scottcurrier.wordpress.com/2010/07/28/simple-wcf-service-within-sp2010-note-to-self/

https://scottcurrier.wordpress.com/2011/06/26/simple-wcf-based-rest-service-for-sharepoint-2010-note-to-self/

Its actually very simple but please see my previous posts for more detail. Assuming you have a separate file for the interface you can return the sample List<Project> by defining a class that looks something like the following

[DataContract]
   public class Project {

       [DataMember]
       public string Name;

       [DataMember]
       public DateTime StartDate;

       [DataMember]
       public DateTime EndDate;
   }

Obviously, this is a simple demonstration – next I implement the actual method as follows

public List<Project> EnumProjects()
        {

            List<Project> projects = new List<Project>();

            // add a new project sample data
            projects.Add(
                new Project {
                    Name = "Project 2",
                    StartDate = DateTime.Parse("3/3/2012"),
                    EndDate = DateTime.Parse("12/15/2012")
                });

            return projects;
        }

The service itself looks as follows (in fact the WebGet attribute can be further simplified)

//http://dev/_layouts/MyServices/MyService.svc/EnumProjects
[WebGet(UriTemplate = "/EnumProjects", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
List<Project> EnumProjects(string webUrl);

For XML the result looks as follows,

– <ArrayOfProject xmlns=”http://schemas.datacontract.org/2004/07/MyServices” xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
– <Project>
  <EndDate>2012-12-15T00:00:00</EndDate>
  <Name>Project 2</Name>
  <StartDate>2012-03-03T00:00:00</StartDate>
  </Project>
  </ArrayOfProject>

For Json the result looks as follows,

[{"EndDate":"\/Date(1355547600000-0500)\/","Name":"Project 2","StartDate":"\/Date(1330750800000-0500)\/"}]

 

Crazy Simple!

Visual Studio Token Replacement Tip – WCF

 

In earlier post I provided one way to enable automatic token replace when building a WCF service for SharePoint 2010 (but this tip applies for any project really), here is what I said

=============

Setup token replacement, see http://msdn.microsoft.com/en-us/library/ee231545.aspx

Essentially, this minimizes the need to manually add things like a 4-part assembly name. After all, the solution already has that information so why not have VS2010 do the work for you.

Navigate to Program files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools

image

Open up the Microsoft.VisualStudio.SharePoint.targets file and locate the TokenReplacementFileExtensions node.

image

Add svc to the list of replacement extensions

<TokenReplacementFileExtensions>$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;svc</TokenReplacementFileExtensions>

=========

The above is ok and works for all your projects except that the file will/could be overwritten by service pack updates. Another way to handle this is to open your *.csproj file and locate the <TokenReplacementFileExtensions> element and add the svc extension directly into the project. Again, see the specifics in http://msdn.microsoft.com/en-us/library/ee231545.aspx

 

Enjoy!

Simple WCF based REST service for SharePoint 2010, note to self

 

This is a follow-on post concerning the building of a simple WCF services in SharePoint 2010, my caveat for this style of post (notes to self) is that the information is intended truly as a “note to self” and so written that way. By now, this info is fully addressed within MSDN and several other folks around the “interwebs”. I’ll simply present the technical differences and some related bits and pieces I find useful – if you find this post useful then all the better.   For now I won’t dive into how the data would be directly returned from a class, perhaps leave that for another post.

Important: Constructing a complete WCF SOAP service is  outlined in an older post, refer to https://scottcurrier.wordpress.com/2010/07/28/simple-wcf-service-within-sp2010-note-to-self/ I’ll concentrate only on the differences for this post. The final result will be a simple WCF service consumable by JavaScript (as well as any other client that can utilize http)

 

Next, refer to http://msdn.microsoft.com/en-us/library/ff521586.aspx

 

The above link outlines the choices regarding WCF Dynamic Configuration for these style of services. The options are SOAP, REST, and ADO.NET Data Service Factories.

In this case we will use MultipleBaseAddressWebServiceHostFactory (REST)

1) So now referring back to my previous post, the first change is to switch the Service Factory to use REST instead of SOAP. Open MyService.svc and include

<% @ServiceHost Language="C#" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory,
                                                                    Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
                                                                    Service="SampleServices.Service1, $SharePoint.Project.AssemblyFullName$" %>

 

2) Because we using the REST Factory we need to “tell” WCF how to handle the Uri by creating a Uri pattern and configure the message format (XML, Json) – the interface will look like this

using System.ServiceModel.Web

namespace SampleServices
{
   
    [ServiceContract]
    public interface IServices
    {

        // http://dev/_layouts/MyServices/MyService.svc/DoWork?name=Scott <== feel free to place the service in ISAPI or LAYOUTS
        [WebGet(UriTemplate = "/DoWork?name={name}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        string DoWork(string name);

     }

 

}

 

Note 1: The results can be returned as XML or JSON

see http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx

 

Note 2: The actual Uri does not look very REST-like because of the pattern chosen in this example. This can be easily modified

See http://msdn.microsoft.com/en-us/library/bb675245.aspx

 

3) The implementation would look as follows

 

namespace SampleServices

{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class MyService : IServices
    {
        public string DoWork(string name)
        {
            return "Hello " + name;
        }

    }

}

What’s left is implementation of a more complex SP 2010 objects that I leave for the reader – but also – don’t reinvent the wheel, SP 2010 comes with plenty of ways with regard to exposing data via services that are compatible with virtually any type of client. The steps outlined in this post allow for simple but truly custom services based on your requirements.  

 

That’s it – Enjoy!

Silverlight Parts Framework–The XapticAPI

 

This is my second post regarding XAPTIC, a web part framework for Silverlight intended for use in SharePoint 2010. The goals of the project are as follows:

  1. A project for learning (of course)
  2. A dead simple way to create Silverlight web parts and use those web parts in SharePoint 2010
  3. The ability to create HTML based web parts that function with the framework as well (for Silverlight 4 I use the overlay method)
  4. Deep integration with SharePoint 2010

I’ll be pushing code up to CodePlex very soon and hoping for your feedback.

 

 

Thanks for watching – Scott