Anonymous Contact Web Part and Code

Allow your users to send an anonymous contact/comment through your SharePoint site

Recently I was asked to allow anonymous comments on a SharePoint site. Keep in mind that this site already had anonymous access enabled and I suggested just opening a discussion and recommending that users post on it anonymously... that was shot down. Soooo... I built this simple little web part that allows the administrator to set up the anonymous contact form. It requires that the admin set four things:

  • SMTP Server - umm... you need this to send email
  • Mailto Address - whoever is going to get these anonymous contacts
  • From address - the dummy address it is 'coming from'
  • Subject Line - what you want the subject line to be

Set those and you are all done -- pretty simple really. All of the code is included in the solution zip, or if you just want the web part, download the wsp. To deploy it, use the command:
stsadm -o addsolution -filename folder_you_put_the_wsp_in/anonymous_contact.wsp
Just be sure to deploy the solution via central admin and add it in your web part gallery with the 'new' button.





"Object reference not set to an instance of an object." when running stsadm backup or enumsites

I ran across this strange error while trying to backup a site collection

I noticed that while running automated SharePoint backups that a certain site collection was not being backed up.  This was a site collection one level down from the root site collection.  For example, if the root site was http://mySharePoint, this was a sub-site collection (sub-SC): http://mySharePoint/sites/subSiteCollection (this was running MOSS 2007 Enterprise).

 

I logged on to the machine and ran the script in a command prompt and it worked fine, it just skipped over the sub-SC.  This got me thinking as to why this was happening, as the script is supposed to enumerate down through all site collections.  First I tried to run a manual backup by running:

 

stsadm -o backup -url http://mySharePoint/sites/subSiteCollection -filename \\backups\subSC.bak

 

And it gave me the error:

 

Object reference not set to an instance of an object.

 

I then tried to enumerate through the sites:

 

stsadm -o enumsites -url http://mySharePoint

 

And all of the other sub site collections showed up fine, but I got this error:

 

<Sites Count="2">

  <Site Url="http://mySharePoint" Owner="DOMAIN\naspinski" SecondaryOwner="DOMAIN\stan" ContentDatabase="WSS_Content_Root" StorageUsedMB="500" StorageWarningMB="0" StorageMaxMB="0" />

  <Site Error="Object reference not set to an instance of an object." />

 

As you can see, the first site shows up, but it is followed by the vague error message where the second site report should be.  This is an incredibly non-descript SharePoint error that leaves you scratching your head.  I knew the site collection was there; I could navigate to it, I could find it in my Central Administration and everything worked fine.  I was able to back up other site collections and everything, just not this one.

 

Turns out after scouring the internet and checking every setting I could find without any help at all, I fell back on one of my List of 5 Simple SharePoint 2007 Fixes (which I should have done first) and number two on the list fixed it.  I ran SharePoint Technologies and Configuration Wizard and magically the problem had stopped.  Once again, not sure exactly why SharePoint did what it did, but this seemed to work out.

List of 5 Simple SharePoint 2007 Fixes

Low-hanging fruit is the easiest to gather; always look for the easy solutions before you move on

I have been a SharePoint 2007 admin/developer for about a year now, and I have learned a LOT.  I have also spent countless hours tracking down strange and seemingly causeless errors in SharePoint only to find out that 50% of my problems required a simple 5 minute (or less) fix.

 

Here is my list, hopefully this will save you time. 

ALWAYS try these before you go on to crazy or overly involved solutions.

 

1. Run an IIS Reset

This is the simplest and often reflects changes that were made that might not show up right away.  Simply open the command prompt on your SharePoint machine and type iisreset.  This will cause momentary downtime (a few seconds) if you are running a single WFE.

 

2. Run 'SharePoint Technologies and Configuration Wizard'

Only #2 on the list because it takes slightly more time than #1.  On the other hand, this has fixed more problems for me than any other on the list.  This does automatic IIS resets, so you will have temporary downtime on a single WFE environment.

 

3. Reboot your machine(s)

This seems obvious, but for some reason is often overlooked.  This holds as true for SharePoint as any other Windows OS/application; the only problem is potential downtime for your users, but if you have more than one WFE, it shouldn't be a problem.

 

4. Make sure account has proper rights

This one is more for stsadm operations and scripts, but most of the time I have found if a command is giving you some strange failurethat it shouldn't, it is more often the account you are using doesn't have the specific right you need for that operation.  The command line runat command is your friend for testing this.

 

5. Install most recent Service Pack/Update

Be sure to check with Microsoft for any updates.  SharePoint is one of their most premier products at the moment, and they are spending a lot of time and money developing for it.

Packaging and Deploying a SharePoint 2007 Web Part

You know how to make a web part, but do you know how to get it to your production environment in the proper way?

In a past post, I did a WSS 3.0/MOSS 2007 Web Part Tutorial that showed how to make a web part.  That's all great and grand, but I did not cover how to actually get it to your production environment (truth is I didn't know how at the time).  Technically, you could install VS 2008 and the extension package on your production farm, but that isn't necessarily a good idea or 'best practice' by a long shot.  What you want is one nice neat file that you can take to your server and upload as a feature, that is what this tutorial is all about (most of this info was deciphered from the SharePoint 2007 SDK.)

 

We are going to assume that you already made your web part, and I am going to use the one from my past tutorial as the example for simplicity's sake.

 

Setup your Solution Structure

In order for this to compile correctly, your web part and cab projects must be inside the same solution.

 

  • Here we go: Open Visual Studio and Choose File > New Project, once in that dialogue, click on Other Project Types > Visual Studio Solutions and choose Blank Solution. Name it something, I am going to use TestSolution for mine.
  • This is a tricky part, but it is to keep the solution folder open and not go to a smaller project when we import one: In the Solution Explorer, right click on the solution and choose Add > New Solution Folder and name it dummy -- we will delete this later, but we need it for a moment.
  • In the Solution Explorer, right click on the solution and choose Add > Existing Project now navigate to your existing csproj (in my case, WPs.csproj) and click OK.
  • Now, in the Solution Explorer, right click on the solution and choose Add > New Project click on Other Project Types > Setup and Deployment and choose CAB Project name it and click OK (I named mine TestCab).
  • Now go back and delete the dummy folder.

 

Now you have the proper setup and structure to your folder.  Only a couple more steps and we will have a nice portable web part.  Your Solution Explorer should now look like this:

 

Prepare your Manifast and dwp files

These are required in order for your cab to be properly set up and used by SharePoint.

 

  • Right click on the Web Part Project inside the Solution and add an XML File, name it Manifest.xml, insert the following text into the file (notice the noly changes you might need to make are to the Namespace, which is used in your .cs file of the webpart, and FileName attributes):

[code:xml]

<?xml version="1.0"?>
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
    <Assemblies>
        <Assembly FileName="WPs.dll">
            <!-- Use the <ClassResource> tag to specify resources like image files or Microsoft JScript files that your Web Parts use. -->
            <!-- Note that you must use relative paths when specifying resource files. -->

            <ClassResources></ClassResources>

            <SafeControls>
                <SafeControl
                  Namespace="WPs"
                  TypeName="*"
                />
            </SafeControls>
        </Assembly>
    </Assemblies>
    <DwpFiles>
        <DwpFile FileName="WPs.dwp"/>
    </DwpFiles>
</WebPartManifest>

[/code]

 

  •  Now right click on the sameProject and add another xml file, name this one your_web_part_name.dwp, for my example I am using WPs.dwp.  Notice that it is the same as specified in the FileName attribute in the xml file above -- add this text to your dwp file:

[code:xml]

<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
    <Title>Title goes here</Title>
    <Description>Put your description here.</Description>
    <Assembly>WPs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5</Assembly>
    <TypeName>WPs.Test</TypeName>
    <!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>

[/code]

 

The only part that may be a snag for some people is populating the <Assembly>'s PublicKeyToken and  <TypeName> tags in the above XML file.  Let me try and explain what and how to get these values:

  • To get the <Assembly>'s PublicKeyToken you need to go to your test machine, where the code was compiled and get the PublicKeyToken - that is the value you enter there.
  • As for the <TypeName> that is simply the Namespace.WebPartName of the web part itself.
  • Now right-click on your CAB Project and choose Add > Project Output once in that dialogue, choose Primary Output and Content Files, Click OK.
  • Right-click again on the CAB Project and Choose Add > File then browse you your dwp file you just made (mine is WPs.dwp), click Open and save your Solution.

 

You should now have everything set up correctly, your Solution Explorer should look like this:

 

Build and Deploy

All the hard work is done, now just build and deploy.

 

  • Right click on the solution and choose Build Solution - this makes your portable web part, just look in your solution folder, in my example it is located at TestSolution\TestCab\Debug\TestCab.CAB - if you will notice, that is just: Solution_Name\Cab_Project_Name\Debug\CabFile.CAB.
  • Now that you have your file, copy it to your production environment to the folder: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin\
  • Then run the command (from that directory):

     

    stsadm -o addwppack -filename Time_Zones.CAB -url http://your_url_here -globalinstall -force

     

  • And run an iisreset.


You are all done.  You should have your web part available to all of your users on the farm now.  And you can pass out the .CAB file to whoever wants it without any messy code to worry about!

SharePoint 2007 Web Part: Time Zones

Displays 6 Time Zones of your choosing, fully customizable

Previously I showed how to build a web part from scratch.  This is my first fully-featured web part.  The javascript was mostly stolen from some unknown source that had been wandering around my shop for years.  Other than that, the web part is all me.  The javascript I was given was just the 6 time zones we use over here (I am working in the Middle East right now) and I got tired of every different division wanting a different set of time zones, so I just made it so they could modify it themselves; now everyone is happy.

 

To install, place the Time_Zones.CAB file into the magical directory of:

 

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin\

 

Then run the command (from the same directory):

 

stsadm -o addwppack -filename Time_Zones.CAB -url http://your_url_here -globalinstall -force

 

Then simply run an iisreset and your web part is ready to use.  The full zip file includes the CAB as well as all the original code so you can modify it if you want.  Otherwise, just download the CAB if you want to simply use the part, enjoy!

 

 

 
 
 
 
 

 

WSS 3.0/MOSS 2007 Web Part Tutorial

So you want to build your own web part eh?

First thing is first, I am going to cut through the crap so you know whether or not you should keep reading right now: for this tutorial to make any sort of sense, you are going to need to have WSS 3.0 and VS/VWD 2008 installed on your Server 2003+ machine; if you do not have one available, make a virtual server (Actually, this is the ideal setup anyway).  You will then need to install WSS 3.0 Extensions for Visual Studio 2008 on your machine (I know I previously posted how to install WSS 3.0 Extensions on your desktop, but that will not work for a full build).  Yes it's a pain, and a lot of requirements, but SharePoint can be a bastard child sometimes.  This really is the hardest part about building new web parts.

Once all of that is out of the way, simply start a new project in VS and choose SharePoint->Web Part.  Give it a name and a location and hit OK.

 

Your solution explorer should now be populated with a bunch of files and references like this:



Now really there are only a few files you need to concern yourself with.  The most important one being the <WebPartName>/<WebPartName>.cs file, in my case it is Test/Test.cs; I will refer to this file as the main file from now on.  I say it is the most important as that is the one that does everything from presentation to code-behind.  The others are the <WebPartName>.webpart file and possibly the AssemblyInfo.cs file, but we will focus on the first right now.


Open up the main file and you will see something like this:



Where you see “TODO: …” is where you are going to put your rendering code.  They even provide you with the always-friendly “Hello World” program if you just un-comment it.  The Guid is automatically populated when you make your project, so don’t mess with it.  The big thing here is that 100% of your styling and layout has to be taken care of back here.  There is no aspx page to your aspx.cs like a normal .net web page.  Once you have a handle on that, it can be treated just like any other asp.net website you have ever made.  There are some other great interactive tools and features you can include, but that is for another day.  Right now, just use the “Hello World” code, use my supplied code or fill it up with your own.

 

NOTE: Often times it is easier to get your programming logic done in a regular Visual Studio Project as there is no way to easily test in a SharePoint Web Part Project.

 

Now that you have your code finished, we can change a few other things.  Open the <WebPartName>.webpart file and you can see some properties you can change.  Customize the Title and Description if you like.



That will change the default settings for this web part.  That is all I would mess with for right now.  So in theory, you now have a 100% working web part.  Now is the easy part.  Just right click on your project and click ‘Deploy’.

 


Assuming everything worked, you know have published your web part to your portal.  If it didn’t work, go fix your bugs and come back :PGo to your Portal->Site Actions->Site Settings->Modify All Site Settings and click on Web Parts under Galleries and you should see your Web Part with a happy little ‘!NEW’ by it.



Now just go to a Web Part Page and put it in there somewhere then exit edit mode.  You should now see your web part in action:


 

Here is my example web part you can take a look at.  It shows a couple different methods of how you can make a web part interact with itself.  As I mentioned above, there is a lot you can do with web parts beyond this, and I will hopefully be able to post some more in the near future.

 

UPDATE

I wrote a tutorial on how to package and deploy a web part once you make it.


Source Code:



MS releases WSS 3.0 Tools: Visual Studio 2008 Extensions

Making web parts for WSS 3.0 and MOSS 2007 just got a lot easier... that is, once you figure out how to install it

Long ago (in computer time) Microsoft offered Visual Studio Extensions for WSS 3.0 web parts and they were wonderful. Problem was for us fools who upgraded to the latest and greatest (Visual Studio 2008) were stuck with a new fancy IDE that did not support this tool that is oh-so-handy. So, in my stubbornness/laziness, I just decided to study up on web parts themselves and hold off on the SharePoint implementation of them until it became compatible, instead relying on the lazy mans way of Jan Tielens' ReturnOfSmartPart (great tool).

But now (actually 8 days ago... we had a big project ok?!) MS has released the new VS 2008 Compatible Extensions and SharePoint devs all over the world rejoice!

But wait!? It won't install on my Windows XP?

It says compatible with Windows XP? But what is this? It also says that a requirement is to have WSS 3.0 installed. Ok, I'll install WSS 3.0, but...

WSS 3.0 will not install on Windows XP. I am no genius here, but let's think about this. If I need WSS 3.0 to be installed in order to install the extensions and WSS 3.0 is not compatible with Windows XP, then by some mathematical property wouldn't it be safe to say that the extensions are not compatible with Windows XP?

Luckily some google searching brought me to Janne Mattila's blog and a way to trick my OS into thinking that WSS is installed. You just need to add/change a registry key:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0] "Sharepoint"="Installed"

Now everything should work fine.

Quickly add and attach multiple content databases to a MOSS 2007 install

A detailed tutorial on how to use some borrowed/modified scripts for making this task very simple

These scripts were stolen from SharepointBlogs.com and have been modified just a little to get some different functionality, I want them to take 95% of the credit... the rest is MINE!  (Thanks guys!)

 

These scripts were almost exactly what we needed, but we needed each content database to have custom names.  So I twisted the code around a bit and here is a detailed tutorial of what we did. 

**IMPORTANT - do this ONLY after you have made your initial web application.

 

Make the Databases

  1. Make a new database named temp_db_builder
  2. Make a table in temp_db_builder with this:
    CREATE TABLE create_dbs(
    id INT NOT NULL PRIMARY KEY IDENTITY, 
    db_name VARCHAR(MAX)
    );
  3. Fill that table with the trailing end of your content db names- EX: WSS_Content_SomeSite : 'SomeSite' = trailing end
  4. Edit make_dbs.sql (from the zip below):
    1. Change @ServiceIdentity and @AppPoolIdentity to the proper accounts
    2. Change the X in WHILE @Number <= X to the number of entries you added in the create_dbs table
    3. Change @DataFile and @LogFile to the proper destinations
    4. That is all that is needed, change the other properties if your situation requires
  5. Run that file, or run the query text in SQL Server Management Studio
  6. Databases are made

 

Produce the Batch file:

  1. Edit the file make_db_batch_file.vbs (from the zip below):
    1. edit NUMBER_OF_DATABASES to the number of names you added in the create_dbs table above
    2. edit SQL_SERVER_NAME to your SQL Server
    3. for however many databases you specified in #1, add the names to dbArray(x) from 0 to NUMBER_OF_DATABASES - these HAVE TO match the names in the table create_dbs
    4. edit DBnameconv to what the prefix of the databases is
    5. edit SiteURL to your site url
  2. Copy that file to your Sharepoint Admin Server to C:\Add_ContentDB_Script\
  3. Go in to your command prompt and run cscript make_db_batch_file.vbs from the C:\Add_ContentDB_Script\
  4. Batch File is made

 

Attach Content Databases to Sharepoint

  1. On your Sharepoint admin server, run a command prompt as the Sharepoint admin account (runas /user:domain\username cmd)
  2. Navigate to C:\Add_ContentDB_Script\ and run AddDBAContentDB.bat

 

And you should be all done.



Content type 'bla-bla' not found in web

This was the error I was receiving after a restore from MOSS 2007 Standard to MOSS 2007 Enterprise

It seems that MOSS is still a bit buggy as I often run across random errors that have no place happening.  I ran into this error when I was migrating a Site Collection from Standard to Enterprise.  All of the other collections went off without a hitch, but this one was giving me problems from the start.  First I want to explain what I think was causing this.  Site Collection B (SCB from here on out) was at a root level on the old farm; it had it's own url and was it's own stand-alone 'portal'.  Now when I migrated, I needed to move SCB under a root collection SCA.  I have done this before, but never run in to any problems, but this was different for some reason?

 

I did my restore with the stsadm command line tool and it cheerfully told me that the 'Operation completed successfully', so I tried to go to the site, but what do I see:

Error

File Not Found.

Damnit... but the site rendered, so I did some checking around.  Everything else on the site seemed to work, if I typed in any other url under that site root, I could get there.  I could get to site settings, to create site, anything, but the main page was bunked out.  I went back to the old version and saw that that page was pretty customized, not to mention not being default.aspx but some odd name that the admins came up with.  So, on a whim, i made a totally blank welcome page (default.aspx) and backed it up, restored, and boom... everything worked.  Not exactly sure why the other page wasn't working, but that is water under the bridge as far as I am concerned, re-making a single page isn't all that tough... sorry users!

 

Now I figure, as long as they can re-make a page, no big deal right?  So I click on Site Actions -> New Page and I get this beauty:

Error

Content type '0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE811' not found in web

Now after countless searched on the internet I turn up basically nothing, TechNet is dry, and no one seems to have had this error (not a first for me sadly).  I ask a desperation post on Experts-Exchange and run through the drills.  Checking my xml config files, looking at error logs, etc.  Then the guy helping me suggests I try something simple... run the SharePoint Products and Technologies Configuration Wizard on all the machines.  Surely it can't be that simple after I bashed my head into the wall for over 24 hours?!

 

It was; just run SharePoint Products and Technologies Configuration Wizard on all your machines and this will go away.  Would that have been so hard to include in the Error message?  Would it?

Updating to SP1 in Sharepoint MOSS 2007 Nightmare

This 'production' service pack that microsoft released is harder to install than my linux wireless NIC

NOTE

This seems to only be a problem if you are already joined to a server farm, not if it is a fresh install

Ok, I love Microsoft, I might even call myself a fanboy, but this Service Pack 1 is a disaster if you are not working with fresh install.  This is the third time I have had to install it and each time it was a mess, so I am writing it down, step-by-step for anyone who has to go through this.  Keep in mind that this particular install was done on a completely naked 6 server farm with no data on it.  Here is the guide:

 

The first service pack must be the WSS 3.0 Service Pack and must be installed on each machine one-by-one, starting with the Central admin machines. 

 

First trying installing it normally (double clicking on it), THIS STEP IS NECESSARY to continue to the other methods. 

 

If that errors out, try this:

  1. Open a command prompt
  2. Navigate to C:\program files\common files\microsoft shared\web server extensions\12\bin
  3. Type psconfig -cmd upgrade -force

 

If that errors, try this from the same command prompt:

  1. stsadm -o upgrade -inplace -forceupgrade

 

If that errors 

  1. Check the Log file produced - if it complains about not having a web.config file:
  2. Copy the web.config file from the server that hosts the Central admin in the directory: C:\program files\common files\microsoft shared\web server extensions\12\TEMPLATE\LAYOUTS
  3. Paste that to identical directory on the error machine
  4. Run stsadm -o upgrade -inplace -forceupgrade again

 

Once that is installed, install the MOSS 2007 SP1 by double clicking on it, that can be done all machines in the farm at the same time.

 

If that fails: 

  1. Open a command prompt
  2. Navigate to C:\program files\common files\microsoft shared\web server extensions\12\bin
  3. Type psconfig -cmd upgrade -force
    • This will look now for the new failed update, even though the process is the same.
    • You HAVE to try to run it normally for this to work

 

Now you are ready to run the hotfix (wss-kb941422-fullfile-x86-glb.exe)

 

This will install by double-clicking the exe, and should not error out.

 

Now you need to run the Configuration Manager on each machine one-by-one.  And you should be done with the upgrade...

 

LINKS: