Thursday 8 January 2009

Remote debugging a SharePoint 2007 web part using Visual Studio 2008

During a recent development project, I had a need to create a web part in C# .NET for use with SharePoint 2007.

When developing the web part, it was necessary to work out how to do remote debugging so I could work out why the web part wouldn't always work.

This post details how I managed to configure remote debugging of a web part project using Visual Studio 2008, connected to a development server running SharePoint 2007.
  1. Firstly, open up your web part project within Visual Studio 2008 on your client PC.
  2. Establish a Remote Desktop Connection to your server, logging in with the same account that you are using on the client PC.
  3. Copy the following folder from your client PC to the server: “C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\x86”. This folder contains the "Visual Studio Remote Debugging Monitor".
  4. On the server, load "msvsmon.exe" from the "x86" folder that was just copied. The Remote Debugging application will load, and display the message "Msvsmon started a new server named 'DOMAIN\UserName@ServerName'. Waiting for new connections." Make a note of the value of 'DOMAIN\UserName@ServerName', as this will be needed in step 16 below.



  5. On the server, share the folder "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin" (where "80" is the port number of the application where your web part resides), giving "Change" permissions to the user account you're using on the client PC.
  6. In Visual Studio, view the Properties of the project, and select the "Build" tab.
  7. Change the "Output path" to "\\ServerName\bin\".
  8. Build the project. The project's DLL will be published straight to the server.
  9. On the client PC, go to "Control Panel" >> "Administrative Tools" >> "Component Services".
  10. Expand "Component Services" >> "Computers", and right-click on "My Computer". Select "Properties".
  11. On the "My Computer Properties" window, select the "COM Security" tab.
  12. Click on the "Edit Limits" button within the "Access Permissions" section.
  13. Tick the "Allow" check box for "Anonymous Logon" to allow the server to connect to the client PC.



  14. Repeat steps 9 to 13 on the server.
  15. In Visual Studio, go to the "Debug" menu and select "Attach to Process".
  16. In the "Qualifier" field, type in the value that was noted in step 4 above.
  17. Tick the "Show processes from all users" check box. This should establish a connection to the server, and display a list of the running processes.
  18. Select all the "w3wp.exe" processes in the list, and click on the "Attach" button.
  19. Visual Studio should enter debug mode.
  20. Load up the SharePoint page in Internet Explorer that contains the web part you wish to debug. You should now be able to set breakpoints in your code and debug your web part!


Problems that you may encounter:
  • If you have a firewall enabled either on the client PC or server, this may prevent the remote debugging from working. Doing a quick Google search should indicate the necessary ports that need to be opened for remote debugging to work successfully.
  • If none of the breakpoints are hit (i.e. Visual Studio says that there are no symbols loaded), then it could be that SharePoint is using the DLL from the "C:\Windows\Assembly" folder, if the DLL was also placed in there, instead of the version in the "Bin" folder. Remove the DLL from the "Assembly" folder and re-add the Web-part in to SharePoint from the "Web Part Gallery: New Web Parts" page to ensure it uses the version from the "Bin" folder.
  • Before the web part can be successfully used in SharePoint, it needs to be registered as "safe". This involves adding a "SafeControl" element to the site's "web.config" file, e.g:

    <safecontrol assembly="MyWebPart, Version=1.2.0.0, Culture=neutral,
    PublicKeyToken=3a2f5d5fb2a8e87a" namespace="MyWebPart" typename="*" safe="True">

    In the web.config file, find the "trust" element, it should look like this:

    <trust level="WSS_Medium" originurl="">

    If you are using "WSS_Medium", load the "WSS_Medium" config file. The default location for this on the server should be "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_mediumtrust.config".

    In the "wss_mediumtrust.config" file, add the section below highlighted in bold:

    <CodeGroup
        class="FirstMatchCodeGroup"
        version="1"
        PermissionSetName="Nothing">
            <IMembershipCondition
               
    class="AllMembershipCondition"
                version="1"
            />

    <CodeGroup
        class="UnionCodeGroup"
        version="1"
        PermissionSetName="FullTrust"
        Name="MyWebPart"
        Description="This code group grants the MyWebPart.dll full trust">
            <IMembershipCondition
               
    class="StrongNameMembershipCondition"            version="1"           
    PublicKeyBlob="002400000489f292a49ba90dfd94fc8"
    />
    </CodeGroup>

    <CodeGroup
        class="UnionCodeGroup"
        version="1"
        PermissionSetName="FullTrust">
            <IMembershipCondition
               
    class="UrlMembershipCondition"
                version="1"
               
    Url="$AppDirUrl$/_app_bin/*" />
    </CodeGroup>

    To get the "PublicKeyBlob" or "PublicKeyToken" values, you'll need to use the "sn.exe" tool, usually found within "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin" on the client PC:

    sn.exe –Tp \\ServerName\bin\MyWebPart.dll

    This will list the "PublicKeyBlob" and "PublicKeyToken" values for the DLL.

No comments:

Post a Comment