Aidan Garnish

Collaboration Not Competition

MOSS 2007 end user training is released

Microsoft have released end user training materials for MOSS 2007. This can be downloaded as a stand alone version that can be run from the desktop or as a version that can be installed and distributed as a set of SharePoint sites. The advantage of installing it to SharePoint is that as a trainer you can direct people towards content and get reports on who has successfully completed which modules.

Download the training here

An efficient way to set web control values

Rather than setting the widths of label and textbox controls individually like this:

oLabel1.Width=150;

oLabel2.Width=150;

oLabel3.Width=150;

It is much more efficient to add the controls to the controls collection and use:

foreach(Control oControl in this.Controls)
{
     if (oControl.GetType().ToString() == "System.Web.UI.WebControls.Label")
     {
       Label oLabel = (Label)oControl;
       oLabel.Width = 120;
     }
}

This then allows you to change the widths of all your labels within the page or web part in one place. This approach can also be taken with other controls like text boxes and drop downs or could be used to set other properties such as font or color.

Or you could just use CSS.  Smile

Setting custom properties on web parts

When creating web parts it is often useful to allow users to supply some additional parameters to the web part. This can be done by adding custom attributes which can then be set by the user and used in your code.

To do this use the following syntax:

   [Personalizable(PersonalizationScope.User),
         WebBrowsable, WebDisplayName("Products List"),
         WebDescription("Use this property to change list used to retrive products from")]
        public string ProductList
        {
            get { return strProductsList; }
            set { strProductsList = value; }
        }

In this case, rather than hard coding the name of the products list, the user is able to specify which list to use.

Using SPQuery and CAML to filter and order

To get a collection of items from a list you can use SPQuery and CAML to filter and order the selected list:

            SPSite oSite = SPContext.Current.Site;
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["ListName"];
            SPQuery oQuery = new SPQuery();
            oQuery.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy><Where>"
                + "<FieldRef Name='Title' /><Value Type='Text'></Value></Where>";
            SPListItemCollection oItems = oList.GetItems(oQuery);

To filter on date use the following to get any item that has been modified in the last 7 days:

string dateString = Microsoft.SharePoint.Utilities.
SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today.AddDays(-7));

oQuery.Query = <Where><Geq><FieldRef Name=\"Modified\"/><Value Type=\"DateTime\">"+ dateString+"</Value></Geq></Where>

Naughty SharePoint Designer

SharePoint Designer keeps adding spaces (&nbsp;) to my HTML in page layouts. This is particularly annoying as I am working to a web design that has been produced at great expense and the customer quite rightly expects that the developed website will match this exactly.

 Unfortunately when I want:

<li>
        <h4><PublishingWebControls:RichLinkField ID="RichLinkField1" FieldName="ByProductType1" runat="server"></PublishingWebControls:RichLinkField></h4>
        <p><SharePointWebControls:TextField ID="TextField4" FieldName="ByProductTypeDesc1"
runat="server"></SharePointWebControls:TextField></p>
</li>

What I get is:

<li>
 <h4>&nbsp;<PublishingWebControls:RichLinkField ID="RichLinkField1" FieldName="ByProductType1" runat="server"></PublishingWebControls:RichLinkField></h4>
 <p>&nbsp;<SharePointWebControls:TextField ID="TextField4" FieldName="ByProductTypeDesc1" runat="server"></SharePointWebControls:TextField></p>
</li>

Hopefully this will be fixed in a service pack but for now I am having to edit my page layouts in Visual Studio and then drag and drop the aspx file into the masterpage library within SharePoint designer. The HTML editor in Visual Studio is much better behaved and does not add in the unwanted spaces.

theKid has found another issue, which I have not experienced, with closing tags being lost by SharePoint designer. Read more about that here...