Brian's profilePoints to sharePhotosBlogLists Tools Help

Points to share

The SharePoint relaed ramblings of Brian Farnhill
2/11/2009

IMPORTANT: Good bye live spaces! Hello Word Press!!!

AS OF TODAY, THIS BLOG WILL NO LONGER BE RUNNING HERE! PLEASE UPDATE YOUR FAVOURITES AND RSS FEEDS!

After almost a year of using Spaces for my blog (being the Microsoft fan boy that I am, I can admit it) I have decided it was time for a change. This was mostly inspired by the fact that I decided I wanted a more professional blog, and while spaces is fine for a basic blog, there are more powerful tools out there for me, and I chose WordPress as the most suitable for me.

Anyway, I have migrated the full contents of this blog to the new address, so eventually I will delete this space but I’ll leave it around for the time being until I’m sure everything is fine. But you should go and check out the new blog and update your RSS feeds!

2/5/2009

How to configure proxy settings for SharePoint

If you have a SharePoint site that is sitting behind a proxy and you are using things like the XML Reader web part to access external content, then you need to configure SharePoint to go through the proxy to get the data. Now fortunately for us SharePoint is based on ASP.NET 2.0 we can configure the proxy in the same was as a regular .NET web app – through the web.config file. Here is a sample:

   1: <system.net>
   2:     <defaultProxy useDefaultCredentials="true">
   3:         <proxy usesystemdefault="False" proxyaddress="http://[proxy address]:8080" bypassonlocal="True" />
   4:     </defaultProxy>
   5: </system.net>
   6:  

All you need to do is to set the proxy address together with the port number into the property there and put that in the web.config and then that’s all you need – problem solved!

2/4/2009

Hiding elements on a publishing page when fields are blank

This has been something that bugs me about most page layouts I have done before – you have a field like the description or something like that and when you put it in you have to put it in a <p> tag to get it to render properly. The problem then is that if you don’t populate the field you then don’t want the <p> tag outside the element to render otherwise you will have some unwanted white space. I had a quick Google around and found a pretty good solution for this at http://social.technet.microsoft.com/Forums/en-US/sharepointecm/thread/dcfc5fe2-5ee1-439a-af44-b59b5c6f59bd/.

Essentially the solution involves creating a custom control based on the security trimmer control, and telling it to trim the components inside it when the specific field is blank. I found the code example at that URL didn’t quite work, so here is my code with some minor adjustments made:

   1: public class HideWhenBlank : SPSecurityTrimmedControl
   2: {
   3:     public String FieldNameToCheck { get; set; }
   4:  
   5:     protected override void Render(System.Web.UI.HtmlTextWriter output)
   6:     {
   7:         SPListItem item = null;
   8:         WebPartDisplayMode mode = ((SPWebPartManager)this.Page.Master.FindControl("wpmMain")).DisplayMode;
   9:         if (mode.Name == "Design")
  10:         {
  11:             base.Render(output);
  12:         }
  13:         else if (SPContext.Current != null)
  14:         {
  15:             item = SPContext.Current.ListItem;
  16:             if (!String.IsNullOrEmpty(FieldNameToCheck) && item != null)
  17:             {
  18:                 if (!ValueIsNullOrBlank(item[FieldNameToCheck]))
  19:                 {
  20:                     base.Render(output);
  21:                 }
  22:             }
  23:         }
  24:     }
  25:  
  26:     private Boolean ValueIsNullOrBlank(Object data)
  27:     {
  28:         if (data == null || String.IsNullOrEmpty(Convert.ToString(data).Trim()))
  29:         {
  30:             return true;
  31:         }
  32:         else
  33:         {
  34:             return false;
  35:         }
  36:     }
  37: }

Compile this class and you can go and use this one in your page layouts like this:

   1: <MyCustomTagPrefix:HideWhenBlank runat="server" FieldNameToCheck="Comments">
   2:     <p>
   3:         <!-- Some sort of custom publishing element in here -->
   4:     </p>
   5: </MyCustomTagPrefix:HideWhenBlank>

So basically in this example when the comments field is empty, then the <p> tag as well as anything else inside the custom tag won’t be rendered. The tag will always allow the content to be rendered in edit mode though which means you can always add a value later. Thsi is nice and easy to implement and can help tidy up your page layouts a bit.

1/30/2009

SPDisposeCheck goes public!

If you are a SharePoint developer, then chances are that you know of the fact that there are several SharePoint objects that always need to be disposed of correctly in order to prevent memory leaks (and if you don’t know about this, then learn about it real quick! There is a good article over at MSDN - Best Practices- Using Disposable Windows SharePoint Services Objects).

Anyway, there has been a tool in the works for a little while now to help developers get this right – SPDisposeCheck. What this tool will do is assess a /NET library (either a DLL or EXE file) and it will tell you about all the lines of code that you need to change in order to meet the best practices for memory management in SharePoint. I’ve seen this tool in action a few months back (wasn’t allowed to say anything about it at that point) and I gotta tell you its pretty awesome. From what we found it was pretty good at picking up any of the issues we had in our code, with a couple of small exceptions and one or two false positives. The point is though that it greatly reduced the likelihood of human error when we reviewed the code because the tool would do it for us, and do it much quicker too.

Now the catch with this tool is that it isn’t officially supported by Microsoft, so don’t go throwing it around on every SharePoint machine you have – but every one of your SharePoint developers should have a copy of this tool on their development environments, and its probably not a bad idea to enforce its use as part of some sort of code review process as well.

So go and download this tool now! It is at http://code.msdn.microsoft.com/SPDisposeCheck

Source: http://blogs.msdn.com/pandrew/archive/2009/01/29/spdisposecheck-v1-3-1-is-released.aspx

1/27/2009

Creating web applications through command line actions

When deploying SharePoint sites I personally am a big fan of the good old batch file - they are simple enough to put together and with a bit of knowledge of the commands that stsadm offers you can create quite a powerful deployment that is reproducible and not prone to human error that you might get when deploying things that require user interaction with the UI.

The scenario that I find is the most common is that you need to deploy your solutions to a SharePoint farm and then create a site that uses them. For example I could have a solution for an intranet site that after I deploy the WSP I would like to create the site based off the templates in the solution. To do this there are a few steps to go through to get this going:

  • Add and deploy the WSP files
  • Create the web application
  • Create the site collection
  • Reset IIS

The first step most people who deploy solutions via command line will be familiar with - the addsolution and deploysolution commands. Basically addsolution is used to store a WSP file in the configuration database so that it can be deployed to the farm, and deploysolution will deploy the solution to the farm from the configuration database. Here is a basic sample of these commands:

   1: stsadm -o addsolution -filename MySolution.wsp
   2: stsadm -o deploysolution -name MySolution.wsp -immediate -allowgacdeployment -allcontenturls
   3:  
   4: stsadm -o execadmsvcjobs

The addsoltuion command is very straight forward, just give it the path to your WSP file and it is a happy camper. The deploysolution command has plenty of options to it, such as allowing the solution to put resources in the GAC, allowing it to deploy CAS policies, which URLs it should be deployed to, and when it should be deployed. This command will create a timer job, and in this case you will want to use the -immediate tag so that when the next line runs the timer job will be executed immediately. Thats it for the first part!

The next thing we want to do is to create a web application. Now I'm assuming here that you know what the URLs and application pool details will be, as you will need them in this command. Here is a sample:

   1: stsadm -o extendvs -description "ShrePoint - My new site -url %internalUrl% -ownerlogin %siteCollectionAdmin% -owneremail %siteCollectionAdminEmail% -databasename %databaseName% -exclusivelyusentlm -donotcreatesite -apidname %appPoolName% -apidlogin %appPoolUser% -apidpwd %appPoolPassword% -apidtype configurableid -allowanonymous
   2:  

The command we use here is extendvs. It will create a web application for us, here are a few things you should know about it:

  • 'description' will be the name of the web applicaiton as you see it in Central Administration
  • 'url' will be the URL of the site in SharePoint. What is worth noting with this is that when SharePoint creates the web application here by default it will ignore the host header in your URL and just use the port number. If you add the -sethostheader option then it will apply the host header for you, so this is usually a must. Also worth knowing is that if you do set the host header then you have to use the application pool username and password options, but I'll explain that below
  • 'ownerlogin' and 'owneremail' are the details of the primary site collection administrator
  • 'databasename' will let you customise the name of the database that is created. There are also options for database server and credentials if you want to configure them too.
  • 'exclusivelyusentlm' tells the new web applicaiton to only use NTLM security. There is also an option to enable kerberos in place of this one
  • 'donotcreatesite' tells the command to not create a site collection in the web application - we will use a seperate command for this that allows us more control over it. You can however do this in one command if you want to
  • 'apidname', 'apidtype', 'apidlogin' and 'apidpwd' all refer to the application pool details (Name, type, username and password of the applicaiton pool identity, in that order). The type attribute can be either 'configurableid' or 'networkservice', use the first if you are using a specific account for the app pool, or the later for just using the network service account.
  • 'allowanonymous' sets the security policy for the site to allow anonymous users

From here we can now create a site collection in our web application, here is a command that will do this:

   1: stsadm -o createsite -url %internalUrl% -title "My new site" -owneremail %siteCollectionAdminEmail% -ownerlogin %siteCollectionAdmin% -sitetemplate STS#0
   2:  

So most of the options here are self explanatory, with the siteTemplate attribute specifying what type of site to create. Once this is done we through in an IIS reset and your new site is good to go

   1: iisreset
   2:  

What I would recommend to anyone who is going to batch deployments like this is to come up with a common file and put all the appropriate variables at the top (you can see the variables in my examples). Then create a copy of the batch file for each environment and and put in the appropriate details into the variables at the top. That way there is less messing around with the batch files at each deployment, which means less chance for things to not be done correctly. Also it might be worth not including the password to your service account in the batch file, and having whoever is responsible for the deployment putting that in manually before it is run, just to keep things a bit tighter from a security perspective. Other than that, your good to go!

And to finish up, here are some references to the commands I’m talking about from TechNet:

 
Microsoft Certified Application Developer
View my MCP transcript
Transcript ID: 690028
Access code: farnhillb

Brian Farnhill

Occupation
Location
Interests
I'm a SharePoint developer, and have been working with MOSS since 2006. I love my technology and gadgets, especailly my Xbox.

Unique World

Unique World
An error occurred loading this module.

A list of presentations I have done, with links to video downloads where possible.
A collection of other MOSS related blogs