Archive for the ‘SharePoint 2007’ Category

Making a web part part 2 – adding XML pages

Thursday, June 3rd, 2010

All three must be added in the project to the folder beneath 12/Template/FEATURES/WEBPARTNAMEFOLDER/

Add feature.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<FeatureĀ  Id=”A GUID”
Title=”… ”
Description=”….”
Version=”1.0.0.0″
Hidden=”FALSE”
Scope=”Site”
DefaultResourceFile=”core”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”elements.xml”/>
<ElementFile Location=”WEBPARRTNAME.webpart” />
</ElementManifests>
</Feature>

Add elements.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”WebPartPopulation” Url=”_catalogs/wp” RootWebOnly=”TRUE”>
<File Url=”WEBPARRTNAME.webpart” Type=”GhostableInLibrary”>
<Property Name=”Group” Value=”GROUP NAME”></Property>
<Property Name=”QuickAddGroups” Value=”GROUP NAME” />
</File>
</Module>
</Elements>

Add WEBPARRTNAME.webpart

<?xml version=”1.0″ encoding=”utf-8″ ?>
<webParts>
<webPart xmlns=”http://schemas.microsoft.com/WebPart/v3″>
<metaData>
<type name=”NAMESPACE.CLASSNAME, NAMESPACE, Version=1.0.0.0, Culture=neutral, PublicKeyToken=faa3e7595fe32ea9″ />
<importErrorMessage>Cannot import webpart.</importErrorMessage>
</metaData>
<data>
<properties>
<property name=”Title” type=”string”>TITLE OF WEBPART</property>
<property name=”Description” type=”string”>Rollup details for all entities.</property>
</properties>
</data>
</webPart>
</webParts>

Opening an SPWEB Object

Thursday, June 3rd, 2010

can be done in one of two ways

SPWeb thesite = CurrentSite.OpenWeb(webGuid);

But you must dispose!

thesite.Dispose();

OR

using (SPSite CurrentSite_d = SPContext.Current.Site)

{

using (SPWeb thesite_d = CurrentSite_d.OpenWeb())
{

//MEMORY IS CLOSED OUT AT END OF CODE BLOCK

}

}

Event receiver worker class – the actual event receiver

Thursday, June 3rd, 2010

namespace KMS_ER_ItemAdding_RS.FeatureCode
{

class KMS_ER_Itemadding_RS_Worker : SPItemEventReceiver

{
public override void ItemAdding(SPItemEventProperties properties)
{

using (SPWeb t_site = properties.OpenWeb())
{

SPList t_aList = t_site.Lists[“Related Systems”];

if (t_site.Name.ToString() == properties.AfterProperties[“RelatedSystem”].ToString())
{
properties.Cancel = true;
properties.ErrorMessage = “You can not relate a system to itself!”;
}

foreach (SPListItem t_listitems in t_aList.Items)
{

if (t_listitems[“RelatedSystem”].ToString() == properties.AfterProperties[“RelatedSystem”].ToString())
{
properties.Cancel = true;
properties.ErrorMessage = “This item already exists in this list!”;
}

}

}
}
}
}

Basic feature xml file

Thursday, June 3rd, 2010

<FeatureĀ  Id=”A GUID”
Title=”SOME COOL FEATURE”
Description=”SOME COOL STUFF”
Version=”12.0.0.0″
Hidden=”FALSE”
Scope=”Site”
DefaultResourceFile=”core”
ReceiverAssembly=”NAMESPACE, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10e084f3a4877340″
ReceiverClass=”FULLNAMESPACE.MAINCLASSNAME”

xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”elements.xml”/>
</ElementManifests>
</Feature>

Adding Event Receivers to all subsites of a certain type

Thursday, June 3rd, 2010

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;

namespace KMS_ER_ItemAdding_RS.FeatureCode
{

class KMS_ER_Itemadding_RS_Main : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

using (SPSite siteCollection = (SPSite)properties.Feature.Parent)
{

using (SPWeb site = siteCollection.RootWeb)
{

foreach (SPWeb subsite in site.Webs)
{

if ((subsite.Name.ToString() == “systems”) || (subsite.Name.ToString() == “hosts”))
{

foreach (SPWeb newsubsite in subsite.Webs)
{
SPList plotList = newsubsite.Lists[“Related Systems”];
string assName = Assembly.GetExecutingAssembly().FullName;
plotList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assName,
“KMS_ER_ItemAdding_RS.FeatureCode.KMS_ER_Itemadding_RS_Worker”);
newsubsite.Dispose();
}
}
subsite.Dispose();
}
}
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{

using (SPSite siteCollection = (SPSite)properties.Feature.Parent)
{
using (SPWeb site = siteCollection.RootWeb)
{
foreach (SPWeb subsite in site.Webs)
{
if ((subsite.Name.ToString() == “systems”) || (subsite.Name.ToString() == “hosts”))
{
foreach (SPWeb newsubsite in subsite.Webs)
{
SPList plotList = newsubsite.Lists[“Related Systems”];

foreach (SPEventReceiverDefinition item in plotList.EventReceivers)
{
if (item.Assembly == Assembly.GetExecutingAssembly().FullName)
{
item.Delete();
break;
}
}
newsubsite.Dispose();
}
}
subsite.Dispose();
}
}
}
}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
}