Elevated Privileges in SharePoint 2007

This is a quick note on incorporating ElevatedPrivileges in custom webparts, event handlers, etc.

Inside the CreateChildControls() method, register the Button Click method reference or any other event.


Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
AddHandler Button1.Click, AddressOf Button1_Click
 End Sub

Inside the Button Event, raise the ElevatedPrivileges in the following way.


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
SPSecurity.RunWithElevatedPrivileges(AddressOf MethodWithElevatedPermission)
 End Sub

Private Sub MethodWithElevatedPermission()
' Add desired business logic here
 End Sub

I have not had the chance to test this on SharePoint 2010 custom application, however I am pursuaded to think it will work for SharePoint 2010 as well providing SharePoint 2010 embraces SharePoint 2007 Object Model.

For C# reference:
http://msdn.microsoft.com/en-us/library/bb466220(v=office.12).aspx

Sharepoint DateTime Column CAML Query

This is a quick post on how to search SharePoint DateTime column using incorporating a DateTime object inside a CAML query.

CAML is Collaborative Application Markup Language is a XML based language that can be used by developers to build, customise and query web sites in SharePoint Services.

You can use SPUtility.CreateISO8601DateTimeFromSystemDateTime to ensure the DateTime object passed by you is in the format acceptable by SharePoint.


Dim CurrDate As String = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now)
Dim _Qry As SPQuery = New SPQuery
_Qry.Query = "<Where>" & _
                "<Eq>" & _
                 "<FieldRef Name='Published_x0020_Date' />" & _
                 "<Value Type='Datetime'>" & CurrDate & "</Value>" & _
               "</Eq>" & _
             "</Where>"

In the code above, I have used a CAML query to find if Published Date column has today’s date. To use SPUtility the following needs to be added

Imports Microsoft.Sharepoint.Utilities

You can also use it like.

http://technet.microsoft.com/en-us/subscriptions/downloads/microsoft.sharepoint.utilities.sputility.createiso8601datetimefromsystemdatetime.aspx

stsadm forcedeletelist in SharePoint 2007

If sometimes Content and Structure part of your Site Collection is not working. You might also observe the following symptoms.

1.         Content & Structure is not working on entire site collection.
2.         Under View All Site Content you see ‘Error’.
3.         Delete Site option is not working.
4.         Sites and Workspaces settings is not working

Reason

There is certain list on SharePoint that is looking for a feature id that have been uninstalled by mistake or retracted via Visual Studio deployment. In my case, I uninstalled a Custom ListType with its Event Receiver without before deleting the list itself. This will result in List existing in SharePoint but missing the prerequisite feature it needs for its basic operations, thus resulting in the symptoms above.

Also, an example when you click on lists from ‘View all Site Content’ you will get error message ‘Missing Features’ highlighting feature id that SharePoint is looking for.

‘aaa7300f-6f68-4aae-9346-76806dd9a59a’ ; ‘d0c4e3fc-9ea9-43a0-819d-f9c1ef87abfc’

Fix

Force Delete these lists from SharePoint either programmatically via code or stsadm operation.

I always keep a test webpart to run any code I wish to on a button click;

Code

targetWeb.AllowUnsafeUpdates = True
Dim badList as SPList = targetWeb.Lists("badlistname")
badList.Delete
targetWeb.Update()
targetWeb.AllowUnsafeUpdates = False

stsadm

stsadm -o forcedeletelist -url http://url/Lists/badlistname or badlist%20name

Hopefully now you should see your Content and Structure restored back to normal.

Integrating Asp.Net Charting with SharePoint 2007

In this post, I want to elucidate developers another useful set of tools in the Asp.Net arsenal that they can use for extending the SharePoint environment by adding visual capabilities or dashboard / data visualisation on the information residing in SharePoint Lists.

Asp.Net Charting Controls

That’s right; using Asp.Net Charting Controls and SharePoint Object Model you can easily create Data Visualisation web parts for your SharePoint environment.

To begin with, please go through the nitty-gritty on the following link

http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx

  • Also bookmark Scott Guthrie’s Blog; it has tons of useful stuff.

This is how I integrated Asp.Net Charting to SharePoint 2007; SharePoint 2010 developers be cautious before following these steps, I cannot validate any of these will work on SharePoint 2010 (although, I have no reason to believe not to; make sure you do the same for 14 hive folder).

  1. Download the charting controls from the link in the post above.We will not require the Visual Studio Project/Solution Tools for our integration.
  2. Browse to – C:\Program Files\Microsoft Chart Controls\Assemblies
    1. Grab a copy of “System.Web.DataVisualization.dll” and add that to the GAC of your SharePoint server aka C:\Windows\assembly folder. Make sure it already don’t exist.
    2. To make sure Charting control utilise the colourful images like (pie, donut, bar, etc) copy the Charting images from MS Chart Project Images Folder to
      1. Browse to C:\Program Files\Common Files\Microsoft shared\Web Server Extensions\12
      2. Create a Folder MSCHARTCONTROLS
      3. Paste the Charting images here
  3. Most important
    1. Modify WebConfig File(s) – If you are using extended site of your web application then both needs to be modified.
    2. Take a back up of your Web Application webconfig
    3. Take a back up of Extended Site webconfig (if any)
    4. Inside the Webconfig look for <WebPartControls> section; inside <SafeControls> grouping
    5. Add “System.Web.DataVisualization” to SafeControl section like below.

<!-- CHARTING -->

<SafeControl Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TypeName="*" Safe="True" AllowRemoteDesigner="True" />

<!-- CHARTING -->

You are now ready to use exciting integration of Asp.Net Charting with SharePoint 2007. In the next post I will illustrate, a sample webpart you can create utilising Asp.Net Charting Controls.

‘sts’ is not recognized as an internal or external command

While deploying custom webpart projects (.wsp files) on your production SharePoint 2007 Application Server when Visual Studio deployment option is not available to you, development teams often resort to the magic ofstsadm for deploying custom solutions on SharePoint Application Server.

Using Dos prompt when you attempt to run this operation on C:\Program Files\Common Files\Microsoft shared\web server extensions\12\bin>stsadm -o addsolution -filename webpartname.wsp

It sometimes results in the following error

‘sts’ is not recognized as an internal or external command, operable program or batch file.

What is happening that the default system paths are not recognising your ‘…./12/bin/’ path. To resolve this issue, do the following (Win Server 2003 R2 SP1 – other Server versions might be roughly the same).

  1. Close your current instance of DOS prompt.
  2. Open Control Panel
  3. Open System
  4. Click Advanced (Tab)
  5. Find Environment Variables
  6. System Variables – look for PATH
  7. Edit
  8. Append the current path with ;C\Program Files\Common Files\Microsoft shared\Web server extensions\12\bin
  9. OK

Now, open your DOS again and try your deployment again.

It should now be successfully deployed.

Deploying custom webparts in SharePoint 2007

In this post, I would like to present the developers with a choice of options that they have at their disposal for deploying custom webparts on SharePoint 2007 environments. The following are a list of web articles that will provide various mechanisms along with a step by step clearly written walkthrough tutorials.

Option 1 Using Class Library Project Type and a Strongly Named Assembly

http://www.codeproject.com/Articles/36792/Developing-and-Deploying-Custom-Web-Parts-for-Shar

Option 2 Using Web part project type and using VS Setup Project for deployment

http://leedale.wordpress.com/2007/05/31/deploying-a-webpart-solution-in-sharepoint-2007-the-simple-way/

Option 3 Using New solution (with webpart files) and a CAB file system

http://naspinski.net/post/Packaging-and-Deploying-a-SharePoint-2007-Web-Part.aspx

Option 4 Using web part project type and deploying with either Visual Studio or stsadm

http://mysharepointwork.blogspot.co.uk/2010/05/deploy-webpart-as-solution-package-and.html