Showing posts with label SharePoint 2007. Show all posts
Showing posts with label SharePoint 2007. Show all posts

Errors After creating new SSP in SharePoint. Cannot Open database. Login Failed

A few weeks back we re-installed our farm and create a new SSP.For some reasons the SSP failed and we had to delete it and re-create it(several times infact).Just After we were done with the installation, our SQL guy handed over a bunch of SQL errors coming out of old SSP's databases. The error generated 4-5 every one minute were
Cannot Open database "Old SSP's Database id" and
Login Failed for NT\NETWORKSERVICE ...

Clearly, some configuration was left out somewhere for these databases and the only task then was to stop getting these errors. After googling a bit and with the help of our SQL guy we found out that there are some SQL jobs(SQL management studio -> SQL server Agent -> Jobs) for these deleted databases which were still running and were trying to communicate with the sharepoint servers.

So disabling the job "TheBadDatabaseId_DeleteExpiredSessions" job under jobs in our Sharepoint SQL instance fixed our issue.

Programmatically Create Site Column, Site Content Type in Sharepoint

Here is a Code snippet for creating a new Site column and adding it the site collection Site column gallery and int0 a newly created content type.

public void CreateSiteColum()
{

string columnGroup = "Custom Columns";

// Adding Custom Column Amount
string amountFieldName = site.Fields.Add("Amount", SPFieldType.Currency, false);

SPFieldCurrency amountField = (SPFieldCurrency)site.Fields.GetFieldByInternalName(amountFieldName);

amountField.Group = columnGroup;
amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
amountField.MinimumValue = 0;
amountField.Update();

See detailed Post @ Create site column in Sharepoint 2010

/* CREATE SITE CONTENT TYPES */

string contentTypeGroup = "Custom Content Types";

// Get a Document content type to be the parent of our new custom content type.
SPContentType documentCType = site.AvailableContentTypes[SPBuiltInContentTypeId.Document];

// Create the Custom Financial Document content type.
SPContentType financialDocumentCType = new SPContentType(documentCType, site.ContentTypes, "Financial Document");
site.ContentTypes.Add(financialDocumentCType);

// Note: A content type is not initialized until after it is added.
financialDocumentCType = site.ContentTypes[financialDocumentCType.Id];
financialDocumentCType.Group = contentTypeGroup;


// Add the Amount column. Child content types inherit the column.
SPFieldLink amountFieldRef = new SPFieldLink(amountField);
financialDocumentCType.FieldLinks.Add(amountFieldRef);

// Commit changes.
financialDocumentCType.Update();

}

site in the above code is SPSite (or SiteCollection) Object.

Programmatically Check if publishing is turned on SharePoint site

In some cases you might wanna check the SharePoint Website to see if the Publishing fetaure is turned on .. well here is a small snippet for that

Public void Checksite(SPweb web)
{
PublishingWeb publishingWeb = null;
if (PublishingWeb.IsPublishingWeb(web))
{
publishingWeb = PublishingWeb.GetPublishingWeb(web);
}
else
{
throw new System.ArgumentException("The SPWeb " + web.Title + " is not a PublishingWeb", "web");
}
}

Note : This code was tested in SharePoint 2010. I guess should work in SharePoint 2007.

Create and deploy Event Receiver Feature and Solution package

Here is a Step-by-Step description of how to create an Item event receiver as a feature and then creating a solution package for it. I this code we will be creating an event receiver for document library types templates where we will assign a value to a custom field called "Location" on adding of a document.

Remember - document added is ItemAdded and if we need to assign any value to document properties ( like Title, File Type etc.) we need to use ItemUpdating.

So lets start.

1. Create a new asp.net Webapplication project and name it as "myEventReceiver".

2. Add Refernces to Microsoft.Sahrepoint.dll in the Project.

3. Create the below two folders in your project

a. ItemEventreceiver

b. Package

4. Add the below three files in the ItemEventreceiver folder (created in step 3)

a. Add a xml file and name it as elements.xml
b. Add a xml file and name it as feature.xml
c. Add a class file and name it as EventReceive.cs

5. Now add another xml file in the project (not in the ItemEventreceiver folder) and name it as manifest.xml.

6. Lastly, add the below two text type files in your project

a. mysolution.ddf
b. mypackage.bat

7. Sign the Project. Generate a key pair using sn.exe tool and save it to your local machine. Now go to Project Properties and in signing tab browse for this key.
Build the Project and get the full name of the dll or assembly from the reflector.

Now lets add some code in these files

EventReceive.cs -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;


namespace EventReceivernamespace
{
public class EventReceiver : SPItemEventReceiver
{

public override void ItemUpdated(SPItemEventProperties properties)
{

SPItem oItem = properties.ListItem;

oItem["Location"] = "New Jersey";

this.DisableEventFiriing();

oItem.SystemUpdate(false);

this.EnableEventFiring();

}
}
}


elements.xml


<?xml version="1.0" encoding="utf-8" ?>
<Elements Id="sfb3c25c-f311-4dd3-86de-29904210c7d5" xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>ItemUpdated</Name>
<Type>ItemUpdated</Type>
<SequenceNumber>2000</SequenceNumber>
<Assembly>myEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9275960caf741af9</Assembly>
<Class>EventReceivernamespace.EventReceiver</Class>
</Receiver>
</Receivers>
</Elements>


Use the fully qualified name of the assembly for this project in your assembly tag.

fetaure.xml

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="2e4d1d48-589e-4328-8a24-a554b2cdcbad"
Title="mycustomImageReceiverFeature"
Scope="Web"
Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml" />
</ElementManifests>
</Feature>



manifest.xml


<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="d7f69035-e74c-44f2-aaa8-928418f6ead0" xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureManifests>
<FeatureManifest Location="ItemEventreceiver\feature.xml" />
</FeatureManifests>
<Assemblies>
<Assembly Location="myEventReceiver.dll" DeploymentTarget="GlobalAssemblyCache">
<SafeControls>
<SafeControl Assembly ="myEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9275960caf741af9"
Namespace ="EventReceivernamespace" TypeName ="*" />
</SafeControls>
</Assembly >
</Assemblies>
</Solution>


mysolution.ddf -


.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=mycustomEventReceiver.wsp -> .wsp name
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=Package

;adds manifest file
manifest.xml

;adds webpart dll
bin\myEventReceiver.dll


;sets the title webpart feature directory
.Set DestinationDir=ItemEventreceiver

;adds the feature manifest to the feature directory
ItemEventreceiver\feature.xml

;adds the element manifest to the feature directory
ItemEventreceiver\elements.xml


Optional
mypackage.bat - This file will create the soltuion package for you and will place it in the Package folder ( that you created in step 3) on build of your project. In addition to that this will also drop the .wsp file in 12 hive's BIN folder (Make sure you have Permissions to drop the .wsp in BIn directory).


@SET MakeCabTool=makecab.exe
@SET WSP=mycustomEventReceiver
@SET DESTDIR="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
@SET SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe

if EXIST Package\%WSP%.wsp del Package\%WSP%.wsp
"%MakeCabTool%" /f mysolution.ddf /D CabinetNameTemplate=Package\%WSP%.wsp

if EXIST Package\%WSP%.cab del Package\%WSP%.cab
"%MakeCabTool%" /f mysolution.ddf /D CabinetNameTemplate=Package\%WSP%.cab

XCOPY /r /y Package\*.wsp %DESTDIR%

Lastly, Edit the Project Properties and under Build Events in "Post Build event command line" box write the below

CD $(ProjectDir)
mypackage.bat

Now build the Project. You should see the mycustomEventReceiver.wsp in your package folder and in SharePoint's BIN directory as well

Now add your solution in sharepoint solution store via stsadm and deploy it.

To do this,

Open the stsadm.exe (from 12 hive bin) and type the below command

stsadm -o addsolution -filename mycustomEventReceiver.wsp

Now got to Cental admin -> Operations-> solution management and click on mycustomEventReceiver.wsp. Click on Deploy and choose one of the web applications that you want to deploy the feature on.

Lastly, Browse to that web application -< Site Settings -< Site features and activate the feature.

To Test -

1. Create a custom document library and create a column "Location" in it.
2. Now upload the document.
3. Once your done uploading and adding properties like Title and stuff for the document you will see the Location column populated with the value "New Jersey"

Yeh!

Sharepoint 2007 development Tutorial

Lets Start with what do you need for your development machine

1. First and foremost, you need Windows Server 2003 R2 with Windows SharePoint Services 3.0 Installed. Then you need to verify that you have Microsoft SQL Server 2005\2008 Developer Edition installed, before you began installing Microsoft Office SharePoint Server 2007 also Known as "MOSS".

Other quick way to start with development is to download SharePoint 2007 VHD from Here and run it with a software called Microsoft Virtual PC. This VHD Contains Microsoft Office SharePoint Server 2007 and Microsoft Office SharePoint Server 2007, you will however need to install VS 2005\2008 to get started.

Once you are all set with the machine you should consider installing Visual Studio Extensions for Windows SharePoint Services 3.0 from Here. This will provide you with various SharePoint Templates to get started.

Let's discuss the knowledge Part - First you need to be familiar with 12 Hive and the Solution structure of SharePoint.

12 hive - SharePoint 2007 have a 12 hive structure which basically contains various files and folders that make SharePoint Interface. The files\components that you will develop will also be deployed under this folder structure.

Solution Package and Feature - Every component that you will develop for SharePoint should be in the form of a Solution Package. These packages are nothing but a .cab files which contain other files that needed to be deployed on the SharePoint server. These Solutions packages represented in SharePoint as .wsp files, also allows you to package Features and defines important metadata about those Features. Besides, Solution Package "Features" is also an important concept in SharePoint development.
Feature is a functional component that can be activated and deactivated at various scopes throughout a SharePoint instances. Almost, every link, button or functional components in SharePoint is a Feature. Take it as a component that can be turned on\off when required. To understand the relation between 12 hive and the .wsp see
12 Hive Folder Structure

Now, lets discuss about SharePoint 2007 Object Model - In SharePoint 2007 Object Model there are two important namespaces .

1. Microsoft.SharePoint namespace - This is the root namespace for all WSS objects. A developer would always use Micorsoft.SharePoint.dll namespace for creating WSS compatible components like webparts,features and solution packages.

2. Microsoft.Office.Server namespace - This is the root namespace of all Office Server 2007 (i.e. MOSS) objects. A developer will use the Microsoft.Office.Server namespace while accessing SharePoint 2007 server (or MOSS) specific components like BDC,Enterprise search etc.

Creating WebParts - Lets Start with creating a very basic webpart using VseWss. This webpart will display some data from a SharePoint list using GridView control on a SharePoint Site Page. The webpart is created as a feature, that means you can activate\deactivate this webpart in your SharePoint site and is deployed in the SharePoint environment as a solution package.
See detail Step-by-Step example Create and Deploy Webpart as Solution Package

Sharepoint 2007 Tutorial for beginners

To begin with, lets look upon some things that you do need to know before you start with the Microsoft Product called "SharePoint".

1. Microsoft Windows SharePoint Services 3.0
- An extentions build on Windows Server 2003, which Provides the infrastructure for collaboration and a foundation for building Web-based applications with this versatile technology. In short, it provides the base for SharePoint Server 2007.

2. SharePoint and .Net
- SharePoint development stack is build upon .Net Framework 2.0 so, it pretty much uses all the .Net Framework 2.0 classes as its base classes. IT however, has its own SharePoint API model, which is commonly used for development.

3. SharePoint and SQL - SharePoint definitely uses SQL server to store the data which is uploaded into it. So be ready with a big SQl space to accommodate SharePoint data.

Now lets Start with the basic definition of SharePoint Server 2007 -
Its a Collaboration software or a Content management Portal which effectively manage and repurpose the content to gain increased business value. In simple words, SharePoint's main purpose is to allow various departments\teams in the company to upload,manage and share documents using SharePoint sites. In addition to Sharing documents,it also allows you to create and manage web sites, use workflows and various other options to implement the business process.For more info see What is SharePoint Used For


SharePoint Administration - As it a server, you would definitely need to learn its administartion. SharePoint has two options avaiable for administration.
1. Central admin - A website available to you immediately after you install SharePoint (and after you ran its Config wizard). This website pretty much allows you to perform and function in SharePoint like Creating\Deleting Websites, Backup\Restore Sites,Set up Users\accounts, Install Custom components and a lot more.

2. Stsadm Tool -
This is a command line tool available in SharePoint, which allows you to perform all the administrative tasks that you can do with Central admin site.

12 Hive - SharePoint stores a lot its Pages and resources on the file system itself for fast retrieval. These Pages or Files which make SharePoint Interface, are arranged in a Folder structure which we call as 12 Hive. The 12 hive gets created at location "C:\ProgramFiles\CommonFiles\Web Server Extentions\12" when you install the SharePoint sever initially. To Know more about 12 hive and SharePoint solution structure see 12 hive Folder Structure

SharePoint Development - SharePoint is also looked upon as a development platform, since all the components in SharePoint, right from a small hyperlink to a fancy button is customizable. You can add your links, buttons , pages and even whole asp.net application SharePoint using its deployment architecture.For more details see :Sharepoint 2007 development Tutorial

SharePoint Object Model -
In Sharepoint Object model there are two Important namespaces.
Microsoft.Office.Server - This is a root namespace of all Office Server objects. This means all the components or services available in Offiice SharePoint Server like BDC, enterprise search etc can be accesed programmatically using this namespace.

Microsoft.SharePoint - This is the root namespace for all WSS 3.0 objects. Remember, Wss 3.0 is the base platform used by Office SharePoint server. For all namespaces see SharePoint 2007 Object model

What is SharePoint made of? - SharePoint Portal is a made of Web applications (WebSites in .Net), Site Collections (a top level Site of the Web application) Web Site Pages,Subsites(or child sites of Site collectons),Webparts,features and more ... See the basic defination for all SharePoint Components that you will use during development Important Definations.

Create site displays only one Site template SharePoint

If Create Site option displays only one Site Template i.e "Publish With Workflow" you might need to select the option for using all the site templates.

To do this, go to Site Actions > Site Settings > Modify All Site Settings then go to Page layouts and site templates under look and feel. Select th eOption "Subsites can use any site template" if you wanna show all the templates or just choose the templates you wanna display. See the screen below :


12 hive folder structure in sharepoint 2007

Few things about 12 hive folder structure and how well it is mapped with the wsp schema.
SharePoint 2007 have a 12 hive structure which basically contains various files and folders that make SharePoint Interface. The files under 12 hive are mostly Uncustomized, that means they stay on the file system rather than in SharePoint database and perform better while rendering the Pages.

You would normally, deploy your Custom files like Custom Pages\layouts, webparts, css or javascript files under 12 hive, so that they can well interact with SharePoint site.

12 hive in SharePoint 2007 has various folders that make up a virtual path for the resources they contain. For e.g to access a CSS file deployed under styles folder we can use a path like
/_layouts/1033/styles/Customcss.css.

The most commonly used folders are :

\ISAPI\HELP\[LCID] - SharePoint help files. Deploy your own .chm files to this folder (or a localized subfolder)

\CONFIG - Web.config customizations

\ISAPI - SharePoint web services (deploy your custom web services here); this maps to /_vti_bin.

\Resources Global .resx - Files accessed from custom features and site definitions

\TEMPLATE\CONTROLTEMPLATES - deploy .ascx user controls here; maps to /_controltemplates

\TEMPLATE\FEATURES - Features contain folder and related file for each feature.

\TEMPLATE\LAYOUTS - Common site pages; maps to /_layouts

\TEMPLATE\IMAGES - Common site image files; maps to /_layouts/images

\TEMPLATE\SiteTemplates - All SharePoint site definitions; create a \xml to deploy your onet.xml custom site definition

\TEMPLATE\THEMES - All UI elements used in themes; clone an existing theme folder to create your own

\TEMPLATE\[LCID]\XML - Webtemp.xml files to define available site definitions; add an xml file here for your custom site definition

\TEMPLATE\ADMIN Pages - Used by Central Admin; maps to /_admin

\ADMISAPI Administration web services; maps to /_vti_adm

Now lets discuss the most commonly used Folder in SharePoint development.

TEMPLATE Folder - The most commonly used folder in 12 hive is the template folder or 12\TEMPLATE\. This is where developers deploy all of there custom files like Custom css, javascript file, Image file, Usercontrol, Custom aspx page, layouts page etc. etc.. These Custom files then run under SharePoint context and can well interact with Out-of-Box sharePoint components.

Now, lets talk about how a wsp (or solution package) interacts with the 12 hive - When you deploy a Solution using stsadm or Visual Studio( using VseWss), SharePoint copies various files from the WSP to the hive. The file responsible in wsp to perform various deployments\copying is the "manifest file".The manifest.xml file specifies how the WSP file is put together.
So, when the deployment starts the sharepoint compiler looks for the first tag i.e the <solution > tag. This tag tells sharepoint about the solution id that uniquely identifies your solution.

Next it looks for the <FeatureManifest> tag. This location however, is virtually mapped to the root of the FEATURES folder at 12 hive i.e to 12\TEMPLATE\FEATURES. So, the SharePoint compiler reads this location and creates a Cutsom Folder usually specified in <FeatureManifest> tag and places the feature.xml file into that folder.

Next it looks for <TemplateFile> tag. This location is virtually mapped to the root of the TEMPLATE folder i.e. at 12\TEMPLATE. This tells compiler to deploy the files specified under this tag, into one or more folder under template directory.
For e.g. if <TemplateFile> tag has location as Location="Layouts\CustomPage.aspx", the installer will drop the file CustomPage.aspx under the Layouts folder, since it is already pointing to the root of the Layouts folder Tempalte folder.

Finally, the installer looks at the <Assemblygt; tag which specifies where the DLL is located. The root for this element is the web application bin file if you specify DeploymentTarget="WebApplication", and the GAC if you specify DeploymentTarget="GlobalAssemblyCache".

While the manifest determines what’s in the WSP file, you still have to create a diamond definition file also known as .ddf file which is an instruction file for building WSP files. However, if you are using VseWSS you will not create all the deployment files manually. The extentions take care of that.

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com