Archive for the ‘SharePoint 2007’ Category

Quick way to add an error or not to ULS logs

Wednesday, February 9th, 2011

Here’s a quick neat snippet to add a comment to ULS logs. Helps to troubleshoot those hard errors.

using microsoft.sharepoint.administration;

try
{
//some code;
}
catch (Exception ex)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0, new SPDiagnosticsCategory(“EMACS CUSTOM”, TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable,
“Writing to the ULS log: {0}”, new object[] { ex.ToString() });
}

Web Part Page Maintenance

Tuesday, June 29th, 2010

In order to access the Web Part Page Maintenance section of a site, add ?contents=1 to the end of the site url.

So http://mysite/sites/mine/?contents=1
There you will be able to remove offending web parts.

Webpart issue: The given assembly name or codebase is invalid

Wednesday, June 9th, 2010

One gigantic issue that I ran into once was creating a web part, adding it as part of solution/feature, putting it on a site and then getting the error “the given assembly name or codebase is invalid”. Well, what I had done was incorrectly set the Assembly name. After changing and redeploying the solution, did it work? Nope.

I continued to redeploy, reset IIS, reboot the machine, several times over. After giving up and rewriting another project, thinking there was some funky bad mojo going on, I saw that the web part I had created was still available in the webpart gallery. Then it hit me, the webpartname.webpart, once added to a site, needs to be deleted even if you redeploy as a solution. Crazy.

Beginning files for any Feature

Thursday, June 3rd, 2010

There are a few files. Feature.xml, elements.xml and in the event of just an eventreceiver, just a main.cs and worker.cs files

Feature.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<Feature  Id=”A GUID IS PUT IN HERE”
Title=”…”
Description=”…”
Version=”12.0.0.0″
Hidden=”FALSE”
Scope=”Site” //can be site/web/farm
DefaultResourceFile=”core”
ReceiverAssembly=”KMS_ER_Itemadding_DetailsLists, Version=1.0.0.0, Culture=neutral, PublicKeyToken=91d8ff9e64e6b4a3″
ReceiverClass=”NAMESPACE.CLASS”

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

Elements.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
//can be blank
</Elements>

main.cs

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

namespace KMS_ER_Itemadding_DetailsLists.FeatureCode
{
class KMS_ER_ItemAdding_DetailLists_Main : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//can do anything you want here. Whatever the feature calls for. This piece of code adds to an event receiver

SPList plotList = newsubsite.Lists[“Details”];
string assName = Assembly.GetExecutingAssembly().FullName;
plotList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assName,
“NAMESPACE.WORKER”);
newsubsite.Dispose();

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
// and to delete it
SPList plotList = newsubsite.Lists[“Details”];

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

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

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

worker.cs

The worker.cs is the logic behind the event receiver. Whatever you just added has to do some logic to determine if it will allow the event to continue or do some other work when it does continue. This one below only allows for one item to be added to a list named Details.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
namespace NAMESPACE
{
class Worker : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
using (SPWeb t_site = properties.OpenWeb())
{
SPList t_aList = t_site.Lists[“Details”];
foreach (SPListItem t_listitems in t_aList.Items)
{
properties.Cancel = true;
properties.ErrorMessage = “Only one item in this list is allowed!”;
}
}
}
}
}

Changing properties of default.aspx through feature

Thursday, June 3rd, 2010

We had a request once to change the width of columns on default.aspx on sites in SharePoint. Though we tried to do this in CSS, there just weren’t any ID tags on the columns, so we couldn’t. For SharePoint designer this is easy to do but we wanted to remove that as making changes with SPD in production is not allowed, and shouldn’t be allowed.

First, we created a feature. In the project we created folders 12/TEMPLATE/FEATURES/DEFAULTMOD/

Added Feature.xml and elements.xml

FEATURE.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature
Id=”GUID”
Title=” MAIN SITE Custom Default file”
Description=”This Feature contains a custom default page for the home page”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”FALSE”
DefaultResourceFile=”core”
ReceiverAssembly=”AAMC_IT_DesignerChanges, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32fc1a05b011eb37″
ReceiverClass=”DesignerChanges.Modifications”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”elements.xml” />
<ElementFile Location=”customDefault.aspx” />
</ElementManifests>
</Feature>

Elements.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”Modifications” Url=”” RootWebOnly=”FALSE”>
<File Path=”customDefault.aspx” Url=”customDefault.aspx” IgnoreIfAlreadyExists=”TRUE” />
</Module>
</Elements>

customDefault.aspx was just changing the middle two columns from the microsoft default of  70/30 to 50/50

Next we created the code that did the swapping out. Notice that when you deactive the feature, the file is just deleted, you can change that and get fancy to back it up as well. You could also do something terribly clever and add the webparts between the files being moved around. We didn’t have that much time and this was for a new site, so we just did a straight swap out.

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

namespace DesignerChanges
{
class Modifications : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
// get a reference to the web
SPWeb web = properties.Feature.Parent as SPWeb;

// back up the original home page
SPFile defaultPage = web.Files[“default.aspx”];
defaultPage.MoveTo(“default-old.aspx”);

// add components to the new custom default page here, if necessary
// move the new default page to default.aspx
SPFile newDefaultPage = web.Files[“customDefault.aspx”];
newDefaultPage.MoveTo(“default.aspx”);

// update navigation, if necessary, here
}

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
SPWeb web = properties.Feature.Parent as SPWeb;

// get a reference to the web

// delete the default page
SPFile defaultPage = web.Files[“default.aspx”];
defaultPage.DeleteAllPersonalizationsAllUsers();
defaultPage.Delete();

// restore the back up
SPFile originalDefaultPage = web.Files[“default-old.aspx”];
originalDefaultPage.MoveTo(“default.aspx”);

}

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

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

BeforeProperties and AfterProperties on SPItemEventReceiver

Thursday, June 3rd, 2010

Originally posted in http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=25

List BeforeProperties AfterProperties properties.ListItem
ItemAdding No value New value Null
ItemAdded No value New value New value
ItemUpdating No value Changed value Original value
ItemUpdated No value Changed value Changed value
ItemDeleting No value No value Original value
ItemDeleted No value No value Null

No value means that column value in the hash table was not available.
New value means that the correct value for the column was available.
Changed value means that the correct updated value was available.
Original value means that the correct original value was available.

Here is the same test against a document library:

Library BeforeProperties AfterProperties properties.ListItem
ItemAdding No value No value Null
ItemAdded No value No value New value
ItemUpdating Original value Changed value Original value
ItemUpdated Original value Changed value Changed value
ItemDeleting No value No value Original value
ItemDeleted No value No value Null

Creating a custom column part 2

Thursday, June 3rd, 2010

Must have TWO cs files, or at least two classes, i like to separate them into separate files. The main CS is really just setting the stage. All the heavy lifting is done in the second class.

MAIN CS

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KMS_Custom_column_Systems.FeatureCode
{
class KMS_CustomColumn_systems_List_main : SPFieldText
{
public KMS_CustomColumn_systems_List_main(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{}

public KMS_CustomColumn_systems_List_main(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{}

public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new KMS_CustomColumn_systems_List_worker(this);

fieldControl.FieldName = InternalName;

return fieldControl;
}
}
}
}

WORKER Class

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace KMS_Custom_column_Systems.FeatureCode
{
class KMS_CustomColumn_systems_List_worker : TextField
{
private KMS_CustomColumn_systems_List_main field;
private HtmlTable table;
private DropDownList dropdownlist;

public KMS_CustomColumn_systems_List_worker(KMS_CustomColumn_systems_List_main parentField)
{
this.field = parentField;
this.dropdownlist = new DropDownList();
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

protected override void CreateChildControls()
{
base.CreateChildControls();

this.table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
table.Rows.Add(row);

HtmlTableCell cell = null;

if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
{

cell = new HtmlTableCell();
row.Cells.Add(cell);

// Create a list selector.
this.dropdownlist = new DropDownList();
using (SPWeb rootWeb = SPContext.Current.Site.RootWeb)
{
dropdownlist.Items.Add(“Select One”);
foreach(SPWeb subsite in rootWeb.Webs)
{
if (subsite.Name.ToString() == “systems”)
{
foreach (SPWeb newsubsite in subsite.Webs)
{
dropdownlist.Items.Add(new ListItem(newsubsite.Name, newsubsite.Name.ToString()));
subsite.Dispose();
}
}

}
}

// Get the current value of the field.
String currentValue = (String)this.ItemFieldValue;

if (currentValue != null && currentValue != String.Empty)
{
this.dropdownlist.SelectedValue = currentValue;
}
else if (this.dropdownlist.Items.Count > 0)
{
this.dropdownlist.SelectedIndex = 0;
}

// Add the script which updates the preview image.
this.dropdownlist.Attributes[“onchange”] = “this.options[this.selectedIndex].value;”;
cell.Controls.Add(this.dropdownlist);
row.Cells.Add(cell);
}

cell = new HtmlTableCell();

LiteralControl literalControl = new LiteralControl();

String logo = null;
object logoObject = this.ItemFieldValue;

if (logoObject != null)
{
logo = (String)logoObject;
}

literalControl.Text =  logo;

cell.Controls.Add(literalControl);

row.Cells.Add(cell);

base.Controls.Add(table);
}
public override void UpdateFieldValueInItem()
{
this.EnsureChildControls();

try
{
this.Value = this.dropdownlist.SelectedValue;
this.ItemFieldValue = this.Value;
}
catch (Exception)
{
;
}
}
protected override void Render(HtmlTextWriter output)
{
this.table.RenderControl(output);
}
}
}

Creating a custom column part 1

Thursday, June 3rd, 2010

This is not creating a custom column through the gui but adding one through the filesystem via a feature.

First add folders 12/template/XML

Add file named fldtypes_XXXXXXXX. MUST ADD fldtypes for SharePoint to recognize this is a new column.

In the case of this colum, the column we are adding is a drop down field. Each item in the column can be clicked to take you to the subsite that you selected.

fldtypes_xxx xml:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<FieldTypes>
<FieldType>

<Field Name=”TypeName”>SystemChooserDropDownList</Field>
<Field Name=”ParentType”>Text</Field>
<Field Name=”TypeDisplayName”>KMS Systems</Field>
<Field Name=”TypeShortDescription”>Displays a list of all systems in the KMS sharepoint site collection.</Field>
<Field Name=”UserCreatable”>FALSE</Field>
<Field Name=”Sortable”>TRUE</Field>
<Field Name=”AllowBaseTypeRendering”>TRUE</Field>
<Field Name=”Filterable”>TRUE</Field>
<Field Name=”FieldTypeClass”>KMS_Custom_column_Systems.FeatureCode.KMS_CustomColumn_systems_List_main, KMS_Custom_column_Systems, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6ecc6dc3171e16e7</Field>
<PropertySchema>
<Fields></Fields>
</PropertySchema>
<RenderPattern Name=”DisplayPattern”>
<HTML><![CDATA[<a href=”http://sharepoint/sites/kms/systems/]]></HTML>
<Column HTMLEncode=”TRUE” />
<HTML><![CDATA[“>]]></HTML>
<Column HTMLEncode=”TRUE” />
</RenderPattern>
</FieldType>
</FieldTypes>

Next up, make the CS files

Making a web part part 3 the CS code

Thursday, June 3rd, 2010

namespace WEBPARTNAMESPACE
{
public class KWEBPARTCLASS: Microsoft.SharePoint.WebPartPages.WebPart
{
//LIVE VERSION
private HtmlTable table;
protected override void CreateChildControls()
{

base.CreateChildControls();
this.table = new HtmlTable();
table.Border = 1;
table.BorderColor = “black”;

HtmlTableRow row = null;
HtmlTableCell cell = null;

using (SPSite CurrentSite_d = SPContext.Current.Site)
{

using (SPWeb thesite_d = CurrentSite_d.OpenWeb())
{
if ((thesite_d.Name.ToString() == “systems”) || (thesite_d.Name.ToString() == “hosts”) || (thesite_d.Name.ToString() == “devices”))
{
foreach (SPWeb subsite in thesite_d.Webs)
{
SPList thelist = subsite.Lists[“Details”];
foreach (SPListItem theitems in thelist.Items)
{

row = new HtmlTableRow();
table.Rows.Add(row);
cell = new HtmlTableCell();
cell.InnerHtml = “System Title”;
row.Cells.Add(cell);

foreach (SPField thefields in theitems.Fields)
{
if ((!thefields.ReadOnlyField) && (!thefields.Hidden) && (thefields.Type != SPFieldType.Attachments) && (thefields.Title.ToString() != “System Name”) && (thefields.Title.ToString() != “Title”))
{

cell = new HtmlTableCell();
if (thefields.InternalName != null)
{
cell.InnerHtml = thefields.InternalName.ToString();

}
else
{
cell.InnerHtml = ” “;
}
row.Cells.Add(cell);

}
}
if (thesite_d.Name.ToString() == “hosts”)
{
cell = new HtmlTableCell();
cell.InnerHtml = “Related Device”;
row.Cells.Add(cell);
}
if (thesite_d.Name.ToString() == “devices”)
{
cell = new HtmlTableCell();
cell.InnerHtml = “Assigned Host”;
row.Cells.Add(cell);
}

}
//break here as we only need the first details list.

subsite.Dispose();
break;
}
}
}
}

using (SPSite CurrentSite_d = SPContext.Current.Site)
{
using (SPWeb thesite_d = CurrentSite_d.OpenWeb())
{
if ((thesite_d.Name.ToString() == “systems”) || (thesite_d.Name.ToString() == “hosts”) || (thesite_d.Name.ToString() == “devices”))
{
foreach (SPWeb subsite in thesite_d.Webs)
{
row = new HtmlTableRow();
cell = new HtmlTableCell();
table.Rows.Add(row);
cell.InnerHtml = “<a href=’http://sharepoint/sites/kms/” + thesite_d.Name.ToString() + “/” + subsite.Name.ToString() + “‘>” + subsite.Name.ToString() + “</a>”;
row.Cells.Add(cell);

SPList thelist = subsite.Lists[“Details”];
foreach (SPListItem theitems in thelist.Items)
{
foreach (SPField thefields in theitems.Fields)
{
if ((!thefields.ReadOnlyField) && (!thefields.Hidden) && (thefields.Type != SPFieldType.Attachments) && (thefields.Title.ToString() != “System Name”) && (thefields.Title.ToString() != “Title”))
{

cell = new HtmlTableCell();

if (theitems[thefields.Title] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = theitems[thefields.Title].ToString();
}
row.Cells.Add(cell);

}

}
//cell = new HtmlTableCell();
//cell.InnerHtml = theitems.Title.ToString();
//row.Cells.Add(cell);
}

if (thesite_d.Name.ToString() == “hosts”)
{
SPList thenewlist = subsite.Lists[“Related Device”];
foreach (SPListItem thenewitems in thenewlist.Items)
{
cell = new HtmlTableCell();
if (thenewitems[“RelatedDevice”] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = thenewitems[“RelatedDevice”].ToString();
}
row.Cells.Add(cell);
}
}
if (thesite_d.Name.ToString() == “devices”)
{
SPList thenewlist = subsite.Lists[“Assigned Host”];
foreach (SPListItem thenewitems in thenewlist.Items)
{
cell = new HtmlTableCell();
if (thenewitems[“RelatedHost”] == null)
{
cell.InnerHtml = “”;
}
else
{

cell.InnerHtml = thenewitems[“RelatedHost”].ToString();
}
row.Cells.Add(cell);
}
}

subsite.Dispose();

}
}
}
}
base.Controls.Add(table);
}

protected override void Render(HtmlTextWriter writer)
{

try
{
SPSite CurrentSite = SPContext.Current.Site;
SPWeb thesite = CurrentSite.OpenWeb();
if ((thesite.Name.ToString() == “systems”) || (thesite.Name.ToString() == “hosts”) || (thesite.Name.ToString() == “devices”))
{
this.table.RenderControl(writer);
//writer.Write(“<a href=\”_layouts/KMSCreateNewEntity/KMSCreateNew.aspx\”>Create new ” + thesite.Name.ToString() + “</a>”);
}
else
{
writer.Write(“This webpart only works with the main pages for Systems, Hosts and Devices in the KMS system. Please delete this webpart now.”);
}
}
catch (Exception ex)
{
writer.Write(ex.Message + ” ” + ex.StackTrace);
}
}
}
}

Making a web part part 1 – basics

Thursday, June 3rd, 2010

First you must create a project in VS. Create folders for 12/template/features/webpartname/<here is where XML files go>

Next create code, best to create in new folder off root of project, webpartcode/webpartcodefile.cs

The feature and elements xml that must be created will reference the webpart.webpart file. That file will reference the actual class where all the work is being done.