Skip to main content

Thinking out aloud - Dave Hunter's SharePoint Blog

Go Search
Home
Blog
  

Locations of visitors to this page


 Useful Links

  Microsoft UK events
  SharePoint 2007 on CodePlex
  SharePoint Community Portal
  SharePoint User Group
  SharePoint Community Portal
  SharePoint Pedia
  SharePoint MSDN Forums
  SharePoint University

 Other Blogs

  Andrew Connell
  Angus Logan
  Arpan Shah
  Bill English
  Charles Emes
  Chris Hines
  From the field
  Heather Solomon
  Joel Oleson
  Lawrence Liu
  Mark Harrison
  Martin Kearn
  Microsoft ECM
  Nick Mayhew
  Patrick Tisseghem
  Penny Coventry
  SharePoint Team
  Steffan Gossner
  Todd Bleeker
Home > Thinking out aloud - Dave Hunter's SharePoint Blog
My thoughts and findings on Microsoft Information worker technologies, including MCMS and SharePoint 2007 (MOSS).
How to check if a SPFile exists in a document library

Usually in SharePoint there are several ways to implement some functionality.  With experience you learn which is the best method.  This is true of retrieving a document from a document library.  At first you think the files must be part of a collection so you look at how to access the SPFileCollection and from that access the document you are interested in.

Approach 1

Typically in .NET when access a collection and retrieve a single item either by name, guid or integer you check the item has been returned before using it. This returns an empty structure and you test if it isn't NULL.  The following example tries this with retrieving a document (SPFile) from a document library.

SPWeb currentSite = SPContext.Current.Web;
SPDocumentLibrary docLib = (SPDocumentLibrary)currentSite.GetList(currentSite.Url + "/Documents"); 

if (docLib.RootFolder.Files[fileName] != null) 
{ 
  // do something 

}

This throws an Argument Exception

  arg_ex

Approach 2

SharePoint has a "Exists" property of the SPFile, this returns a boolean flag to indicate if the file exists.

SPWeb currentSite = SPContext.Current.Web;
SPDocumentLibrary docLib = (SPDocumentLibrary)currentSite.GetList(currentSite.Url + "/Documents"); 

if (docLib.RootFolder.Files[fileName].Exists == true) 
{ 
  // do something 
} 

Again this throws a Argument Exception

 arg_ex

Approach 3 

The correct way of checking if an item exists is using the GetFile method of the SPWeb.  The example below illustrates this.

if (currentSite.GetFile(fileName).Exists == true)
{
  // do something
} 

 

Summary

When retrieving a SPFile from a document library use the SPWeb.GetFile() method and test if it exists using SPFile.Exists.

How to programmatically set the content type while creating a list item

If you create a new list item using the API without setting the content type id you will create a new item using the default content type.  However if you want to create a new list item using a different content type you will need to set the Content Type ID field.

The example below creates a list item using a content type called "Log" in a list called "HistoryLog".

using (SPSite site = new SPSite("http://intranet/"))
{
  using (SPWeb rootWeb = site.OpenWeb())
  {
    SPList historyLog = rootWeb.GetListFromUrl("/lists/historylog/allitems.aspx");
    SPListItem item = historyLog.Items.Add();
    item["LogType"] = "Warning";
    item["LogMessage"] = "Message .....";

    // set the content type id
    item["Content Type ID"] = rootWeb.ContentTypes["Log"].Id;
    
    item.Update();
  }
}
SharePoint Designer - stop that lag when loading

SharePoint Designer (SPD) by default loads the last site you were using when it opens.  I know this is a "feature" and has it benefits when you are working on one site, but I find myself realising that its opening the last site and frantically clicking the stop button in hoping that it will stop loading swiftly.

stop

You can turn this feature off, select Tools > Application Options

You will be presented with the following

spd_options

De-select the first option and click OK.

Timer will not run error because the administrative service is not enabled error

I booted up my server this afternoon and tried to deploy a SharePoint solution package. I ran a deployment script which called stsadm commands to install and deploy several wsp files. The install script looks something like:

SET stsadm ="%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"

%stsadm% -o addsolution -filename "MySolutionPackage.wsp"

%stsadm% -o execadmsvcjobs

%stsadm% -o deploysolution -name "MySolutionPackage.wsp" -allowgacdeployment -allcontenturls -immediate –force

%stsadm% -o execadmsvcjobs

Nothing special is happening here. After both the addsolution and deploysolution commands I get the following error:

The timer job for this operation has been created, but it will fail because the administrative service for this server is not enabled. If the timer job is scheduled to run at a later time, you can run the jobs all at once using stsadm.exe -o execadmsvcjobs. To avoid this problem in the future, enable the Windows SharePoint Services administrative service, or run your operation through the STSADM.exe command line utility.

I checked the services and found out that the Windows SharePoint Services Administration service wasn't running. After starting the service the errors weren't reproduced.

Please note: My install script included the command execadmsvcjobs which runs all jobs that are waiting to be run instead of waiting for the service to pick it up. If you deploy a solution via Central Administration you should run STSADM –O execadmsvcjobs or start the Windows SharePoint Services Administration service.

Just need to find out now why the service didn't start automatically .... TBC

Re-published my MOSS Content Types Viewer on CodePlex
I've re-published my Content Types Viewer on CodePlex.  This release includes full source code (currently it's POC standard, I will update soon).  http://www.codeplex.com/MOSS2K7CTypesViewer
 
For more information please see:

Andrew Connell has a great extension for generating site columns and content types which can be downloaded here http://www.andrewconnell.com/blog/articles/MossStsadmWcmCommands.aspx

My tool was developed before this was available and was aimed to be a visual tool for reengineering existing content types.

Visio Shapes for SharePoint

I was looking to expand my toolbox for designing SharePoint solutions, I've found the following Visio Shapes, enjoy!

Shapes for wireframes

Source

Description

TamTam - Ferry

Shapes for wireframing page level functionality. Download it here http://www.den-dopper.com/2008/10/14/visio-template-and-stencil-for-designing-sharepoint-moss-2007-portals-and-sites/

Nick Finck

This is a generic set of IA shapes but still relevant. Please see http://nickfinck.com/stencils/

 

Shapes for designing and planning SharePoint farms and their structure

Source

Description

Microsoft

Site hierarchy planning tool is part of Microsoft's planning worksheets for MOSS and WSS. For more info http://technet.microsoft.com/en-us/library/cc288346.aspx

SmallSteps.co.nz

Similar to the above but includes lower level shapes for documents, tasks, calendars etc. Download it here http://www.smallsteps.co.nz/articles/VisioStencil.aspx

Maxime Bombardier

Site structure shapes for designing or documenting MOSS. For more information and to download please see http://blogs.msdn.com/maximeb/archive/2007/10/27/updated-visio-stencils-for-sharepoint-2007-site-structures-documentation-version-0-2.aspx

SharePoint vNext – Rumours, Speculation and Confirmed Features

The table below lists the known facts about what we can expect in SharePoint vNext and their probability of happening or making the version.

Feature

Summary

Probability

Source(s)

64 bit only

SharePoint vNext will be a 64 bit version only.

CONFIRMED

TechNet

Silverlight

Silverlight 2.0 webparts or UI will be present.

MOST PROBABLY

Speculation

Super-Lists

SQL tables-like behaviour for SharePoint lists

PROBABLY

Bill Gates

Groove Integration

If the user has Groove client installed, more options will be displayed for data synchronization, in more seamless way.

PROBABLY

Ray Ozzie

Master Data Management

Master data source for keeping only one version of the truth. This data can be surfaced as SQL Server views or SharePoint data. In essence, a rebranded and somewhat expanded version of Stratature product +EDM, now known as Codename "Bulldog".

MOST PROBABLY

Wikipedia
Microsoft MDM

XHTML-compliant output

SharePoint UI will produce clean XHTML-compliant output.

PROBABLY

Speculation

FAST search integration

FAST-based enterprise search as a Search replacement. Webparts that show FAST search results.

MAYBE

CMS Watch

ODF and PDF support

Custom filters won't be necessary to index and extract metadata from ODF and PDF files.

PROBABLY

Microsoft

CMIS support

Content Management Interoperability Services will allow SharePoint to communicate with other ECMs via web services.

MOST PROBABLY

Microsoft

Claims-based Authentication mechanism

Decouples the authentication mechanism from its implementation. It will enable SharePoint to use any interoperable authentication mechanism to authenticate the users.

MAYBE

Network World

 

Thanks to Edin for compiling this list http://edinkapic.blogspot.com/2008/10/sharepoint-v14-2009-feature-list.html.

We've known about the 64 bit feature for some time now and have been recommending 64 bit server deployments ever since (for both performance reasons and future proofing customers environments). Microsoft have released some more information about CMIS here http://blogs.msdn.com/ecm/archive/2008/09/09/announcing-the-content-management-interoperability-services-cmis-specification.aspx.

 

Use InfoPath to author XML schemas (XSD)

From guowu

It is very easy to use InfoPath as an XML schema editor:

  1. Choose Design a form… from the File Menu in InfoPath.
  2. In the Design a new form task pane, select New Blank Form. Note, if you want to derive a schema from existing XML document or data connection, choose one of the New from… options and skip to step 4) below.
  3. On the Design Tasks pane, click on Data Source to show the Data Source Pane. Right click on the myFields element, you will see a shortcut menu. Starting there, you can create data source structure that models your need.
  4. Once you are done, go to File menu, Extract form files…, then quit InfoPath. Go to the folder you just saved extracted form files, you should find myschema.xsd is what you want. Note: if you are deriving a schema, the schema you want may not be defined directly inside myschema.xsd, but imported there.
  5. (Optional) Open myschema.xsd in your favorite XML or text editor, remove the my namespace auto-generated by InfoPath or replace it with your desired namespace URI.

An innovative use of InfoPath J

K2 BlackPearl - Update Design Templates

When you upgrade your K2 environment to the latest release (for example 803), you can update a process developed in a previous version by updating the design templates. You can do this by:

  • Please see the note below before continuing.
  • Right clicking on the design surface
  • Select "Update Design Templates" (pictured below).
  • You can then select the top node to update everything or specify particular activities etc to update and then click Finish

K2 BlackPearl - Update Design Templates

Note: What it doesn't tell you is that the process of updating the design templates will lose any server code in the process. Make sure you backup the process before you update the design templates. When the update is complete you will need to copy and paste the server code back in place.

I found this post on K2 Underground confirming this behaviour http://www.k2underground.com/forums/thread/20602.aspx.

Tip - SharePoint 2007 Escaped Column Names

Most developers who have experience of SharePoint will know that when naming columns using the user interface will result in the actual column name will be an escaped version of what the user entered as the column name. The most common replacement is a space is replaced with "_x0020_". For example: A user enters "Published Date". The display name will remain as "Published Date" but the underlying name of the column will be "Published_x0020_Date".

The table below lists some of the commonly replaced characters

Character

Hex Code

Escaped Version

[space]

20

_X0020_

|

7C

_X007C_

-

2D

_X002D_

\

5C

_X005C_

(

28

_X0028_

)

29

_X0029_

'

27

_X0027_

,

2C

_X002C_

!

21

_X0021_

%

25

_X0025_

&

26

_X0026_

?

3F

_X003F_

$

24

_X0024_

£

A3

_X00A3_

"

22

_X0022_

<

3C

_X003C_

>

3E

_X003E_

=

3D

_X003D_

#

23

_X0023_

 

However there is another way, .NET has a method called EncodeName, which is part of the System.Xml namespace. You can use this method to take in a string that needs to be escaped and returns the escaped string based on the input. For example:

 
using System.Xml; 


// encode the column name 
string escapedString = XmlConvert.EncodeName("Published Date"); 

 
// decode the escaped column name  
string normalString = XmlConvert.DecodeName(escapedString); 

For more information about EncodeName please see http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.encodename.aspx. Also note that there is a DecodeName method that does the reverse.

One way to get around the escaping is to name a column without spaces or special characters, save the changes and then go back into the column and rename with your desired name. The result is that the name specified first is used for the column name and the latter just changes the display name.

Hope this helps

1 - 10 Next
  Copyright
This work is licenced under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License
  Site Map

 Dave Hunter

I'm currently a Senior Consultant at Netstore 2e2. I specialise in Microsoft Information Worker technologies (especially MOSS, MCMS and .NET), with over 8 years of experience within this area of specialism. 

I have gained two certifications for MOSS and WSS and look to complete the full certification track soon.

View Dave Hunter's profile on LinkedIn

 Subscribe in a reader


 Latest Posts

How to check if a SPFile exists in a document library11/11/2008 10:58
How to programmatically set the content type while creating a list item03/11/2008 12:14
SharePoint Designer - stop that lag when loading31/10/2008 12:18
Timer will not run error because the administrative service is not enabled error29/10/2008 17:29
Re-published my MOSS Content Types Viewer on CodePlex26/10/2008 18:16
Visio Shapes for SharePoint25/10/2008 19:30
SharePoint vNext – Rumours, Speculation and Confirmed Features24/10/2008 17:36
Use InfoPath to author XML schemas (XSD)12/06/2008 15:23
K2 BlackPearl - Update Design Templates06/06/2008 13:49
Tip - SharePoint 2007 Escaped Column Names 04/06/2008 13:27
1 - 10 Next
Posts that contain SharePoint per day for the last 30 days.
Technorati Chart