Thursday, March 25, 2010

Creating, Modifying SharePoint Views Programmatically

Creating a New View
String[] colArray = new String[] { "Column1", "Column2", "Column3” };
StringCollection colCollection = new StringCollection();
String viewQry = “Your CAML Query Here’;
SPView newView = YourList.Views.Add(“View Title”, colCollection, viewQry, 100, true, false);

Note: 100 is the item limit, true is allow paging and false is to set as the default view

Adding Fields to an Existing View
SPList oList = oWebsite.Lists["List_Name"];

SPView oView = oList.Views["All Items"];
SPViewFieldCollection collViewFields = oView.ViewFields;

Adding a "Group By" to the View
String query =  "";
view.Query += query;

Tuesday, March 23, 2010

String Manipulations in ASP.NET

Find String within string
This code shows how to search within a string for a sub string and either returns an index position of the start or a -1 which indicates the string has not been found.

string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);

Strip specified number of characters from string
This example show how you can strip a number of characters from a specified starting point within the string. The first number is the starting point in the string and the second is the amount of chrs to strip.

string MainString = "S1111tring Manipulation";
string NewString = MainString.Remove(1,4);

Thursday, March 18, 2010

Creating Lookup, Multi-Value Fields in SharePoint

Because lookup fields are not included when you save a list as template in SharePoint, you might need to create custom code to programmatically add these fields to a list during site provisioning. Here's how:

SPSite site = new SPSite (siteURL);
SPWeb web = site.OpenWeb();
SPList departmentList = web.Lists["Department"];
SPList employeeList = web.Lists["Employees"];
employeeList.Fields.AddLookup("Department", departmentList.ID, false);

Lookup field in this case is the Department field which was added to the Employee list. Afterwards, you may set this field as a multi-value field:

SPFieldLookup categoryField = (SPFieldLookup)eLibrary.Fields[fldName];
categoryField.AllowMultipleValues = true;

Reading XML Documents

XmlDocument doc = new XmlDocument();
XmlNode root = doc.DocumentElement;
string url = root.SelectSingleNode("url").ChildNodes[0].Value;

Thursday, March 4, 2010

Custom Field Types in SharePoint

Minimum requriements:

A field type definition. This field type definition is an XML file that contains the information that Windows SharePoint Services needs to correctly render the field, including its column header, on list view pages (such as AllItems.aspx). It is typically also used to render the field on the view list item page (DispForm.aspx) and sometimes it is used to render the field on the New or Edit (list item) pages. It can also declare and define special variable properties of the field type whose values will be set whenever a column is created based on the field type. Most importantly, it contains information about the assembly that includes the compiled field type.

A field class. This is a class whose instances can represent particular fields that are based on your custom field type. This class must inherit from SPField or one of the classes in Windows SharePoint Services that derive from it. The class is compiled into a strong-named assembly and you deploy it to the global assembly cache. In the context of a list view, an SPField object represents a column and its properties, such as whether it can be sorted. In the context of the Display, New, and Edit modes, an SPField object represents a particular field of a list item — a cell in the table that constitutes the list — and the value of that cell in the content database.

A rendering control class. This is a class that can be used, in conjunction with a rendering template (see below), to render your fields in New mode or Edit mode, or, less commonly, in Display mode. This class must inherit from BaseFieldControl or one of the classes in Windows SharePoint Services that derive from it. This class is compiled into the same assembly as the field class.

A rendering template. The rendering template is defined in an .ascx file located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\ControlTemplates.

An editing control for the variable properties of the field type. All field types require a name, a data type, a description, and other common properties; but many field types also have properties that are relevant only to fields of that particular type. These variable properties are set by users in the UI when they create a new column that is based on the field type. Usually, an element in the field type definition (see above) determines how these property setting controls are rendered. But sometimes a special editing control is required. Such a control is defined in an .ascx file that usually has a code-behind file that contains its logic. Creating a special editing control is recommended if you need to perform custom functions, such as complicated computational logic, looking up values from data sources, or custom data validation of the values that a user might choose when configuring a new column.


Wednesday, March 3, 2010

Event Handler Explorer

Determine whether an event-handler has been successfully attached to your lists or document libraries:

Signing an Assembly in .NET

Here are steps in order to sign an assembly using Visual Studio and be able to deploy that to the GAC:
  1. Open the Project within Solution Explorer
  2. Right-Click and Select Properties
  3. Switch to the "Signing" tab
  4. Select the option to "Sign the Assembly"

Lutz Roeder's .NET Reflector

The.NET Reflector is a class browser and decompiler that can examine an assembly and show you just about all of its secrets. The .NET Framework introduced reflection which can be used to examine any .NET-based code, whether it is a single class or an entire assembly. Reflection can also be used to retrieve information about the various classes, methods, and properties included in a particular assembly. Using .NET Reflector, you can browse the classes and methods of an assembly, you can examine the Microsoft intermediate language (MSIL) generated by these classes and methods, and you can decompile the classes and methods and see the equivalent in C# or Visual Basic® .NET.

You may download here.