This site is a self-contained low down on what's going on in my life, what I'm working on, what I'm thinking about, and how I'm feeling about life in general.

Wednesday, 05 November 2008

In the SharePoint implementation I'm working on, we have a site collection for each department. Each department has variables such as an ID and other information that other webparts require. 

Previously, I'd just provided the option to specify these variables in the webpart controls. This worked but was very time consuming to populate when creating a new department.

I then came across away to add new properties to the site collection that can be called from any webpart. In my example, I'm submitting a form containing the values I want to set. In this case it's just a textbox with a department ID in it.

try

{

SPContext.Current.Site.RootWeb.Properties.Add("CurrentDeptID", _currentDeptID.Text);

SPContext.Current.Site.RootWeb.Properties.Update();

}

catch (Exception ex)

{

SPContext.Current.Site.RootWeb.Properties["CurrentDeptID"] = _currentDeptID.Text;

SPContext.Current.Site.RootWeb.Properties.Update();

}

Now I know what your thinking, why have I called the SPContext class on each line instead of creating an object. Well, there is a good answer. When working with the contextual site in the "correct" manner, I had an error each time about trying to use an SPWeb object that had been disposed of. This was not the case and after digging around found that this is a known issue, so adjusted my code to suit.

Anyone who reads my posts will see that I'm struggling by in asp.net, having had no formal training I need to fall back on logic in the face of more efficient code, so if there is a better way to do this, please let me know.

The try{} catch{} are used to trap whether an add event fails. You can only add a property once, so if it fails just try to update it.

Properties are retrieved in the same way.

_currentDeptID.Text = SPContext.Current.Site.RootWeb.Properties["CurrentDeptID"];

This makes a huge difference to productivity, saving us hours a week, give it a go!



Tags: Microsoft Site SharePoint Webpart Webparts C# C#.net Properties Rootweb Collection Site_Collection

Thursday, 31 July 2008

 

This article is aimed at those developers who've developed their own SharePoint 2007 webparts in Visual Studio using the wss sdk and want to deploy them to a different server.

1. Build it

The first step is to do a standard Visual Studio deployment of your webpart (F5 or the play button). Once this has happened go to the project folder, then the bin > debug folder.


2. Take it

In here you should have a file along the lines of my_projects.wsp. Take this file and copy it to a folder on one of the web front ends you are deploying on.


3. Script it

Next you need to create two files in the same folder ideally as your .wsp file.

Create a new text document and call it DeploySolution.cmd, then add the following lines to it, replacing my_solution.wsp with your solution name.

 

@setlocal
@pushd.

@set PATH=C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN;%PATH% @cd %~dp0

stsadm.exe -o addsolution -filename my_solution.wsp
stsadm.exe -o deploysolution -name my_solution.wsp -allcontenturls -allowGacDeployment -immediate
stsadm.exe -o execadmsvcjobs

@pause
@popd
@endlocal

 

Once you have done that, create another text document and call it RetractSolution.cmd, then add the following lines to it, again, replacing my_solution.wsp with your solution name.

 

@setlocal
@pushd.

@set PATH=C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN;%PATH% @cd %~dp0

stsadm.exe -o retractsolution -name my_solution.wsp -local -allcontenturls -immediate
stsadm.exe -o deletesolution -name my_solution.wsp
stsadm.exe -o execadmsvcjobs

@pause
@popd
@endlocal

 

Use the first script to deploy your solutions on the server. If you need to update the solution, first use the retract solution script, then re-run the deployment script.

Once you've run the script, you'll most likely have to go into Site Settings > Site Collection Features and turn on the webpart.



Tags: Web SharePoint Webpart Part 2007 Deploy Load Send Dwp Script stsadm

Thursday, 19 June 2008

I’ve been trying to create two webparts that communicate with each other in SharePoint. Looking around the web there are a number of good examples of how to create an asp.net 2.0 webpart that communicates with another but there are a couple of things that they don’t tell you.

Develop your webparts in one project

If you want your webparts to be able to talk to each other, you have to develop them in the same project in Visual Studio, otherwise they won’t compile. Sounds obvious enough but no-one ever actually says to do this.

Once you’ve built the provider webpart, simply add a new webpart into the solution explorer and off you go.

Linking the webparts

Another thing that is never covered for SharePoint is how to link the webparts once they’ve been developed. Firstly, click edit page, and then click the edit button on either the provider or the consumer webpart.

Screenshot of sharepoints communication options

From here you will see connections, select send ... to, or get ... from, depending on which webpart you’re looking at. Once this relationship has been established, your webparts should communicate perfectly.



Tags: Microsoft Web ASP.NET SharePoint Webpart Part Communication Communicate Link 2007 Develop

Friday, 30 May 2008

I can honestly say that writing a .net 2.0 webpart for sharepoint 2007, that has the sole function of reading data from a database, has been the hardest thing I've ever done in my life!

It took me 14 hours through a combination of trail and error to get it working, finding very few resources online, and not being able to use the coveted visual tools available to asp.net developers. Anyway, I got there, and I'll share how with you.

To start with, the tools used. I'm using visual studio 2005, and have the wsssdk installed, the sharepoint 2007 sdk, and the wss 3.0 extenders for Visual Studio 2005.

Start up Visual Studio and create a new webpart. It'll be called webpart1 by default, lets leave it at that for now. First off, right click the project in the solution explorer, and click add reference. Scroll down and select System.Data. Once you've added this we're ready to begin.

Now, make your code look like this:

using System;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace WebPart1
{
    [Guid("493c926d-f885-4965-b72d-ee18e0292630")]
    public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string _sqlStatement = "SELECT * from tblTest";

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("SQL Statement"),
        WebDescription("Query to return a set of data")]
        public string SQLstatement
        {
            get { return _sqlStatement; }
            set { _sqlStatement = value; }
        }

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(SQLstatement)
|| !SQLstatement.ToUpper().TrimStart().StartsWith("SELECT")
|| SQLstatement.Contains(";"))
            {
                Literal lit = new Literal();
                lit.Text = "Only single SELECT statement allowed"; Controls.Add(lit); return;
            }

            DataGrid grid = new DataGrid();
           
            //Attempt connection
            try
            {
               
                using (SqlConnection conn = new SqlConnection("server=SP-TEST\\databasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
                {
                    SqlCommand cmd = new SqlCommand(SQLstatement, conn);
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    grid.DataSource = reader;
                    grid.AutoGenerateColumns = true;
                    grid.DataBind();
                    reader.Close();
                    conn.Close();
                } Controls.Add(grid);
            }

            catch (Exception exp)
            {
                Literal errMessage = new Literal();
                errMessage.Text = exp.ToString();
                Controls.Add(errMessage);
            }

        }

    }
}

Now just build the control and your webpart should be working well. Lets take a look at some of the key parts.

private string _sqlStatement = "SELECT * from tblTest";

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("SQL Statement"),
        WebDescription("Query to return a set of data")]
        public string SQLstatement
        {
            get { return _sqlStatement; }
            set { _sqlStatement = value; }
        }

This part sets the SQL query you're about to run.

using (SqlConnection conn = new SqlConnection("server=SP-TEST\\databasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
                {
                    SqlCommand cmd = new SqlCommand(SQLstatement, conn);
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    grid.DataSource = reader;
                    grid.AutoGenerateColumns = true;
                    grid.DataBind();
                    reader.Close();
                    conn.Close();
                } Controls.Add(grid);

This part connects to the database and displays the results in a data grid. Note the server name SP-TEST\\databasearea has two backslashes, this is to counter the escape effect of a backslash.

So that's it really, tiny bit of code with big implications. Hope it opens some doors for you.



Tags: .net ASP.NET SharePoint Webpart SQL Visual_Studio

Wednesday, 28 May 2008

Lets get something straight, I hate Microsoft SharePoint.

Not because it's a bad product or it's expensive licensing, it's because I'm a web developer being bullied and backed into a corner by it.

I actually think SharePoint is good for some things, its Active Directory integration, it's ease of use and powerful feature set, but it should know it's place. 

Microsoft may lead you to believe that it's all powerful with it's Master Pages and .net 2.0 framework construction, but in truth, it is not a good platform to build custom applications into. Even what should be a no brainer for something so powerful, connecting to an SQL database and returning tabulated data, is an absolute chore of the highest calibre.

I'm tasked with building bespoke solutions for a SharePoint portal, and the only way I can do this is through what are called webparts. These webparts are actually quite powerful, if a little limited, in that to build a multi-page application, you need to build lots of webparts and piece them together in multiple sharepoint pages.

It's a total bind, but I'm warming to them, looking at the positives I can deploy a web part across a huge number of different web portals very quickly, without duplicating any code. That's it for the positives so far, I'll let you know how I get on....



Tags: .net SharePoint Webpart Webparts SQL Custom Bespoke Development Portal

Monday, 26 May 2008

I thought that a new job and a new baby together would be a clean break all-round. As it turns out it's about the hardest thing I've ever been through, and on top of it all I've got a cold.

But enough moaning, on to some updates.

Firstly, week two at work is over, and things are getting better! More structure, more idea where things are going, more settled and more vision for the future.

On the back of work, I'm learning asp.net, amongst other things. I always knew it would be a huge leap going from asp 3.0 to .net, and there is that ever-present, inherent fear of the unknown to contend with. Now I have no choice but to learn it, and thankfully it's not as hard as I thought. In fact I'm enjoying it, which definitely helps.

Outside of work it's been a tough week. The amount of work that goes into raising a baby is starting to sink in and take its toll, and I'm only doing a quarter of the work (if that). Lisa has been amazing taking care of him, but it's still draining. You just don't have time for anything, and sometimes knowing that just eats away at you, even if you don't have anything to do anyway. I can only update my blog at crazy times of day. It's a tough lifestyle change but I'm getting used to it. Single parents have more than my admiration, they have my vote for sainthood. 

The run of failing electrical items has progressed today as I ordered a replacement fridge freezer. The old one doesn't keep milk for more than a couple of days, bugger. That's now the Xbox, the Camcorder, The Tumble dryer, and the washing machine that have bitten the dust recently, as if things weren't strained enough.

Another positive is that I found a little motivation, and a little time to start working on a few project ideas. Unfortunately I have little of both and things haven't worked out very well so far, but progress is progress. I want to build something to help me save money, and will probably make it into a web service, all singing all dancing. I'm also working on a VLE of my own, probably open source. Hopefully it will be my first asp.net project release, although I plan a php version also.

Well the room is starting to rock back and forth which means one thing, need sleep.



Tags: Work Baby Lisa Michael Tired Sleep Progress ASP.NET Silverlight SharePoint Open-Source VLE Electrical Moaning
Featured Articles
Recent Articles