Paging error when programmatically adding gridview.

When adding the gridview programmatically to a webpage / composite control, you must add the gridview to the controls heirachy before databinding. If you do not, paging breaks giving the error: “ERROR: Object reference not set to an instance of an object.”

Im pretty sure that this is only the case when the gridview datasource is set by the “datasource” property and not by refering to a sqldatasource control using the “datasourceid” property. I experienced this problem when trying to use a mysql datasource.

The code below illustrates the correct ordering:

DataSet ds = CustomGetDataFunction();
GridView grid = new GridView();
grid.AllowPaging = true;
grid.PageIndexChanging += new GridViewPageEventHandler(gridPageIndexChanging);
grid.DataSource = ds;
this.Controls.Add(grid);
grid.DataBind();

Rough Notes On Web Controls

Just to get it clear in my head, i though id write a few rough notes on creation of web controls. There are three types of web controls in ASP.NET 2.0:

1. Custom Rendered Controls
These derive from the System.Web.UI.WebControls class. To create a rendered control, you simply override the Render method and generate output using Writer.Write(text) where text contains html and writer is of type HtmlTextWriter (passed to the render method).

Note that instead of writing html using Writer.Write(), it is often recommended that you use the inbuilt HtmlTextWriter methods. My personal preference, however, is not to use these – as I like to actually see the html in the code. The benefit of using the HtmlTextWriter is apparently that it includes support to hide the facets of switching between different DTDs (eg html 3.2 and 4.0).

The downside to Custom Rendered Controls is that you cannot reference other asp controls method. If you wish to do this, you will need to create a Composite Control.

2. Composite Controls
Composite controls are composed of other controls. Composite Controls derive from the System.Web.UI.WebControls.CompositeControl class (although they still compile if they inherit just from System.Web.UI.WebControls – I believe the CompositeControl class just effectively adds the INamingContainer interface to the derivation list.

Instead of overriding the render method, you override the CreateChildControls method. Controls are added to this as follows:

Label lblMessage = new Label();
lblMessage.Text = "Hello World";
this.Controls.Add(lblMessage);

Note that you still can add custom html directly – e.g. this.Controls.Add(new LiteralControl(“Hello World“));

3. User Controls

Essentially, users controls can be thought of like aspx forms, although instead of forming a page, they form a control that can be tracked ontol the page. Derives from the System.Web.UI.UserControls class. Note that the user control class must exist in a .ascx file, otherwise compilation may fail. When creating a user control in VS2005, you can drag and drop other controls onto it as if you were creating a normal aspx form.

Dynamically adding controls to a composite control
Whereas you can dynamically add controls to a composite control based on events, due to the execution order of controls you cannot wire up that control to events – see
http://weblogs.asp.net/vga/archive/2003/08/11/23498.aspx
http://www.code-magazine.com/article.aspx?quickid=0305101&page=2
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp

Note that to some extent, you can simulate dynamic adding of controls by setting the visiblity of a control to false when initially rendering.

* Edit
The only way around this is: on the event that creates the dynamic control(s), set up a flag in viewstate (to indicate that the new control(s) are to be created) and whenever the page_load / createchildcontrols runs (after the event to create controls has initially run) call the event to create the dynamic control(s) (making the call from page_load / createchildcontrols). Note that you probably do not want to dynamically call the actual event (due to having to pass args) so instead set up a function that is call both by the event and by page_load / createchildcontrols.
Note that when an event is raised (on an aspx page, I havent actually tried this on a control) the running order is on_init, initialise_component, page_load, then the event being fired. You therefore cannot simply set the viewstate flag in the event and then subsequently generate the control on page load (as the flag to show the dynamically created control will not be set).

VSTO, VSTA notes

VSTO – Visual Studio Tools for Office

VSTA – Visual Studio Tools for Applications – similar to VBA (Visual Basic for Applications), although there is no plan yet to phase out VBA.
Note that the Microsoft Trinity team is responsible for both VSTO and VSTA.

VSTO V3 is in the pipeline – providing support for all office apps and server programming, but will not be out with Office 2007.
Instead, VSTO Cyprus will apparently be release with Office 2007. This permits creation of plug ins to ribbons, custom panes and form regions etc. The beta of this is apparently available now.

RAD – Rapid Application Development

SharePoint 2007 – Various Notes and Ramblings

  • Office 2007 is according to Microsoft the biggest office release for the next 10 years.
  • Note that you still cannot put code behind a SharePoint page, although as with previous SharePoint releases these pages can reference existing dlls. Note that you can put asp.net code inline though. I’m not 100% sure whether this was possible in SharePoint 2003 / wss2 as this ran through an isapi filter (although some basic code such as response.write was possible) – SharePoint 2007 runs straight through asp.net.
  • WSS3 does not ship with and shared services (e.g. advanced search, excel services, InfoPath server, bdc – note to self: need to confirm exactly what’s not provided.
  • SharePoint now has multi value lookup fields.
  • You can also now have folders in lists.
  • SharePoint lists can all apparently receive emails (a new item is created when they do).
  • Calendars can be overlaid in outlook (SharePoint and outlook calendar – although they don’t fully sync).
  • New projects tasks list – lightweight project solution. Not meant to compete with MS Project. This list has a gant chart view.
  • There is now extensible support for custom email handlers – Microsoft.SharePoint.Utilities.SPEmail
  • Apparently now, you should just be able to append /m/ to any site to view the mobile version of the site.
  • Important files in a feature: Features.xml – declares feature and apparently specifies other dependencies. Elements.xml – list what is packaged in feature

In terms of development, the core areas of Sharepoint are:

  1. SharePoint Data – via the object model
  2. Meta Data – definitions and content types
  3. Behaviours – Events (synchronous and non-synchronous – i.e. can now affect whether an item does get placed in a doc lib etc depending on the outcome of an event), Workflows
  4. ASP.NET extensions – Webparts, custom controls, custom pages (not sure where these would reside yet)
  5. Packaging – Features (sharable across site-defs)

Content Types
It would appear that in a list, when generating content types, you can only inherit once from a global content type – therefore you cannot have two content types based on one global content type. I asked a Microsoft guy about this and apparently it is related to the way in which global content types are identified (apparently as an guid concatenated with another sequential number). I’m not really clear on this – and need to look into this further.

SharePoint Applications
At the SharePoint 2006 conference in Seattle, Bill Gates talked about there being loads of pre-configured SharePoint 2007 “Applications” in his keynote (e.g. a Help Desk, Fault Reporting app etc). I have not really seen evidence of these, other than a couple of new definitions (e.g. request tracking and project lists). From speaking to a Microsoft guy, it seems that at some point there may be a load of these preconfigured templates available (as per the 30 or so already available for WSS2 at http://www.microsoft.com/technet/prodtechnol/sppt/wssapps/default.mspx). Note that it is likely that these wont be in the actual base product.

InfoPath in relation to SharePoint.
InfoPath is useful for collecting data for through web-service, apps or just storing it as an xml file. In the context of SharePoint 2007, data collected from InfoPath can either be stored in a forms library or in an form-based content type. In these scenarios, the input metaphor is either client side or server-side InfoPath (not a SharePoint form).

Now, seeing as InfoPath forms can now be rendered in the browser if the user doesn’t have InfoPath installed, i would think that the next logical step would be to allow all SharePoint forms to be overridden by InfoPath forms. This does not seem to be the case however (although it may be possible to hack this in). I wonder why in the context of SharePoint, custom forms cannot just be designed in SharePoint designer (eliminating the need for InfoPath forms server with SharePoint), such that all forms are just rendered in html all the time.

Note that with infopath, some of the controls cannot be rendered in html, so infopath forms server does not support all controls. Note when designing a form, you can specify whether you want it to be html compatible.

Sharepoint 2007 – Warning applying root masterpage

Warning: The selected master page has no approved version. This site may appear to be broken to users without the view versions right in the Master Page Gallery.

This simply means that the master page you are trying to select (from the master page gallery) has not been approved – simply change to approved by right clicking the item and approving (note that you will need to ensure that it is checked in and may need to “publish major version” before you can approve it). Note that you will only see the approval link if you have the correct credentials.

Master Pages In Sharepoint 2007 (beta 2)

I really like master pages in asp.net 2.0 and really liked the idea of them being used / available to the end user in Sharepoint 2007. From what I initially saw, I assumed that site collections would have a master page gallery (which could be used by all subsites). I was also hoping for propergation of masterpages through site hierachies (which had not had an alternative masterpage applied). This does seem to be the case – but not everywhere.
The way in which master pages are applied to sites supporting alternate master pages is as follows:

  1. Make a copy of the sites default.master (from the masterpage gallery). You can obviously create a new master page from scratch, but there is a danger that you may forget to include important Sharepoint controls).
  2. Edit you copy master page in visual studio / frontpage / notepad etc. Be careful not to when stripping out references to Sharepoint controls (normally youll probably want to leave most of these in)
  3. Upload your new masterpage to the masterpage gallery (off of site settings), be sure to ensure that this new masterpage is approved.
  4. Go to the site.settings page of the relevant site. Select the Site Master Page Settings link under the look and feel section.
  5. Apply the new master page to the site and refresh your browser. The master page will be applied.

Now, this is what really puzzles me – all subsites seem to have their own masterpage gallery, but there is not an option on the relevant site settings page for Site Master Page Settings. This makes me ask why is there a gallery if the only think that is ever used is default.master. Note that this could just be something that is resolved in Sharepoint 2007 RTM, or it may simply be a way of letting you alter the hardcoded master page (if so this seems like a bit of a cludge).

The only mechanism for changing master pages on sites that do not allow alternate master pages, appears to be by simply editing the default.master file. This can be done either by accessing it through the masterpage gallery or by navigating to it in Sharepoint Designer 2007. I believe that in such sites, the reference to the masterpage is hardcoded to default.master in the page definitions (specified by MasterPageFile="~masterurl/default.master" in the page directive). Note: the article http://channel9.msdn.com/showpost.aspx?postid=197664 tells you to directly edit default.master.

The table below summarise which out of the box site templates support master pages, and to what extent…

Group
Template
Has Master Page Gallery
Can Set Master Page
Inherits From Parent Site Master Page

?
Root Site
Y
Y
N/A

Collaborations
Team Site
Y
N
N/A

?
Blank Site
Y
N
N/A

?
Document Workspace
Y
N
N/A

?
Wiki Site
Y
N
N/A

?
Blog
Y
N
N/A

?
Records Repository
Y
N
N/A

?
News Home Template
Y
Y
Y

?
Publishing and Team Collaboration Site
Y
Y
Y

?
Publishing Site
Y
Y
Y

Meetings
Basic meeting workspace
Y
N
N/A

?
Blank meeting workspace
Y
N
N/A

?
Decision meeting workspace
Y
N
N/A

?
Social meeting workspace
Y
N
N/A

?
Multipage meeting workspace
Y
N
N/A

Enterprise
Document Center
Y
N
N/A

?
Personalisation Site
Y
Y
N

?
Site Directory Area Template
Y
Y
Y

?
Report Center Site
Y
Y
Y

?
Search Center Site
Y
Y
Y

Note that on sites that only have a default.master page, all settings pages use the application.master page – this is global and unfortunately cannot be overridden.

SharePoint – Deleting all global templates via stsadm

Deleting all global sharepoint templates has been a real pain for me – especially when I’ve had 50+ registered. There is no switch on stsadm to delete all templates – you can only delete them one-by-one. I hence thought id brush up my dos scripting this lunchtime and write a command to facilitate deleting of all global templates registered through stsadm…

stsadm -o enumtemplates > templatelist.txt & for /F "tokens=1" %i in (templatelist.txt) do stsadm -o deletetemplate -title %i & del templatelist.txt

Note that this can actually go on three lines…

stsadm -o enumtemplates > templatelist.txt
for /F "tokens=1" %i in (templatelist.txt) do stsadm -o deletetemplate -title %i
del templatelist.txt

Note that either stsadm must be in the path, or you must be in C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN (or equivalent) when running this command. Remember to do an iisreset when run.

Also note that I believe this works in both sharepoint 2003 / wss 2 and sharepoint 2007 / wss 3

Summary Of New Features In SharePoint 2007

Here is a brief outline of the new features in SharePoint 2007 – I will hopefully go into these in more detail very soon. If I have missed any, please give me a shout…

  • Item level permissions
  • Content management
  • Content types
  • Master Pages
  • Multi Lingual (mysites etc)
  • Workflow
  • Aggregation Points
  • Blogs, Wikis and RSS
  • Business Data Catalog (search works across this)
  • Features (new means of deploying custom code)
  • New Interface
  • Recycle Bin

SharePoint 2007 Search

Some key points…

  • Apparently the search algorithm uses Click Distance, Anchor Text, URL Depth and an Metadata to index data. Results are grouped by social distance
  • The search is capable of suggesting similar terms.
  • Duplicate elimination.
  • Can search with WSS 3.0 – previously WSS2 could only use SQL Server Search
  • Can index any ldap v3 directory
  • A crawl log is kept
  • You can apparently tweak certain aspects of the search algorithm (by changing weighting??)
  • Results are security trimmed whatever PAM (Pluggable Auth Module) is used
  • You are able to remove single items from the search index (without having to re-index)
  • The search can index non-SharePoint sources – e.g. from the business data catalogue
  • You can set which properties are picked up by the search index crawl
  • Search SDK – apparently this will be available within the Beta 2 timeframe
  • You can change the way results are presented by tweaking the XSLT and CSS
  • The search crawl can propagate continuously