Tuesday, July 28, 2009

Data Import to a Sharepoint List

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
namespace DataImport
class Program
static void Main(string[] args)
// Check there are enough command line parameters
if (args.Length <>[ []]");
} char[] splitArray = { ',' };
int startRecord = 0;
int endRecord = int.MaxValue;
// Store the command line parameters.
string paramFileName = args[0];
string paramWeb = args[1];
string paramList = args[2];
// Get start record integer.
if (args.Length > 3)
if (args[3] != String.Empty)
startRecord = Convert.ToInt32(args[3]);
// Get end record integer.
if (args.Length > 4)
if (args[4] != String.Empty)
{ endRecord = Convert.ToInt32(args[4]);
// validate the start and end record
if (endRecord < startRecord)
Console.WriteLine("End record parameter must be greater than start record parameter");
// open the CSV file for reading
using (StreamReader fileRdr = File.OpenText(paramFileName))
// connect to the site collection
using (SPSite spSite = new SPSite(paramWeb))
// open the root site
using (SPWeb spWeb = spSite.OpenWeb())
// get the list from the site
SPList spList = spWeb.Lists[paramList];
// read the fields in the header
string line = fileRdr.ReadLine();
string[] fieldNames = line.Split(splitArray);
// get an array of field from the list
// this assumes the field names match
// we need the field Id when inserting rows
SPField[] spFields = new SPField[fieldNames.Length];
for (int looper = 0; looper < fieldNames.Length; looper++)
string strName = fieldNames[looper];
SPField field = spList.Fields.GetField(strName);
spFields[looper] = field;
// get the next line
line = fileRdr.ReadLine();
// start counting from the 2nd line
int lineCount = 0;
// keep reading till we run out of file
while (!fileRdr.EndOfStream)
{ // if we are past or on the start record then
if (lineCount >= startRecord)
{ // a little bit of progress
if (lineCount % 10 == 0)
// get the values from the line
string[] fieldValues = line.Split(splitArray);
// create a new list item
SPListItem newItem = spList.Items.Add();
// populate the values in the item using the
// SPFields we created earlier
for (int looper = 0; looper < fieldValues.Length; looper++)
newItem[spFields[looper].Id] = fieldValues[looper];
// update the item to the list
// see if we want to stop prematurely
if (lineCount >= endRecord) break;
//read the next line
line = fileRdr.ReadLine();
if (fileRdr.EndOfStream)
{ string[] fieldValues = line.Split(splitArray);
// create a new list item
SPListItem newItem = spList.Items.Add();
// populate the values in the item using the
// SPFields we created earlier
for (int looper = 0; looper < fieldValues.Length; looper++)
newItem[spFields[looper].Id] = fieldValues[looper];
// update the item to the list

Monday, July 27, 2009

Where to use WSS Object Model

Object Models - Sharepoint

Windows SharePoint Servies 3.0Microsoft Office SharePoint Server 2007

Document Libraries in Sharepoint

WSS Document Library is a special list type
  • Provides support for uploading/storing documents.
  • provides support for document versioning
  • Document data is stored in Microsoft SQL server content Database
  • Like a list with one(and only one)attachment
  • Document Library has a document template
  • Document template used when user clicks New command
  • User Content Types for multiple document templates
  • Sharepoint Lists

    List types designed to store content in form of items
  • Each list type defines schema including set of columns
  • List type includes Form pages(eg.,AllItems.aspx)
  • List type can have multiple views
  • Creating a List instance from a List type
  • Create list items using WSS built-in List Types.
  • Create List items from Custom Lists and define columns
  • Users can Add/Remove/modify columns on list instance.
  • List can contain Content Types
  • Content Types contain their own fields,workflows, and forms
  • Content Type fields are automatically added to a list when the Content Type is associated with it
  • Creating a Sharepoint Theme

    1. Copy and rename the existing theme.
    2. Rename the info file of the theme with the given name of the copied folder.
    3. Open the .inf file and replace the titles.
    4. Customize colors, fonts and so on in the style sheets of the theme.
    5. Add your own images to the theme.
    6. Create a thumbnail and copy it to the 12\TEMPLATE\Images folder.
    7. Add theme definition in the SPTHEMES.xml in the 12\TEMPLATE\Layouts\ folder.

    SharePoint List Template Id’s

    When creating SharePoint 2007 receivers, the ListTemplateId attribute is associated with a “value” that is an optional unique identifier for the target template for which the receiver is being built. The value of templates are as shown below.
    1200Administrator tasks list
    104Announcements list
    303Blog Categories list
    302Blog Comments list
    301Blog Posts list
    105Contacts list
    120Custom grid for a list
    118Custom Workflow Process
    130Data Connection library
    110Data sources
    108Discussion board
    101Document libraryaEvents list
    150Gantt Tasks list
    100Generic list
    1100Issue tracking
    103Links list
    114List template gallery
    116Master pages gallery
    201Meeting Agenda list
    202Meeting Attendees list
    204Meeting Decisions list
    207Meeting Objectives list
    200Meeting Series list
    210Meeting text box
    211Meeting Things To Bring list
    212Meeting Workspace Pages list
    117No-Code Workflows
    2002Personal document library
    109Picture library
    300Portal Sites list
    2003Private document library
    111Site template gallery
    107Tasks list
    112User Information list
    113Web Part gallery
    119Wiki Page library
    140Workflow History
    115XML Form library

    Friday, July 24, 2009

    Introducing Master Pages in Sharepoint

    * Central file controlling the layout and design of the content pages in a sharepoint site
    • Title, logo and description of the site
    • Navigation controls
    • Welcome and Administration
    • Search experience
    • Linking cascading style sheet(CSS) files and script files
    • Web part Manager
    • Contentplaceholders for sharepoint products and technologies

    Setting Master pages programatically

    • Execute code to switch to new provisioned Master page
    • Feature Receiver

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    SPWeb site = Properties.Feature.Parent as SPWeb;
    Site.Properties["OriginalMasterPage"] = site.MasterUrl;

    CSS Files

  • CSSLink control loads core.css file
  • Override Styles
  • with an Explicit embedded <style> element
  • programitically setting the AlternateCSSUrl property of the SPWeb
    Public override void FeatureActivated(SPFeatureReceiverProperties properties)
    SPWeb site = properties.Feature.parent as SPWeb;

    Thursday, July 23, 2009

    Singleton Pattern

    Using System;
    Using System.Text;
    Namespace Singletonproject
    Class Singleton
    Private Datetime CreatedDateTime;
    Private Singleton
    CreatedDateTime = System.DateTime.Now;
    Private static Singleton instancia = null;
    Private Object Padlock = new object();
    Public Singleton Instancia
    instancia= new Singleton();
    return instancia;
    Public string GetDateTime
    return CreatedDateTime.Tostring();
    Calling procedure
    Singletonproject.Singleton clsc= Singletonproject.Singleton.Instancia;
    Label1.text = clsc.GetDateTime.Tostring();

    Finding the Id (Guid) for a SharePoint List

    * Navigate to the SharePoint list using the browser.
    * Select the Settings + List Settings menu command.
    Copy the Url from the browser address bar into Notepad. It will look something like: http://moss2007/ProjectX/_layouts/listedit.aspx?List=%7B26534EF9%2DAB3A%2D46E0%2DAE56%2DEFF168BE562F%7D
    Delete everying before and including “List=”.
    Change “%7B” to “{”
    Change all “%2D” to “-“
    Chnage “%7D” to “}”
    You are now left with the Id:

    Thursday, July 16, 2009


    C# 3.0 includes many strong new features, but one of the most interesting is the inclusion of its new query keywords, that you use to create Query Expressions. Query Expressions are most commonly associated with Language Integrated Query (LINQ). They're the core syntax that you'll use when you create LINQ queries, whether you use LINQ to Objects, LINQ to SQL, LINQ to XML, or some other custom LINQ provider. I'll discuss the query keywords, how those keywords map to methods defined using the query operands, and how you can define your own custom implementation for the query keywords. The C# team worked to minimize the necessary grammar for LINQ and the associated query syntax. They ended up adding eight contextual keywords to the C# language specifically for query: from, where, select, group, into, orderby, join, and let.

    int[] someNumbers = { 2, 3, 7, 11, 13, 17, 19 };
    var query = from number in someNumbers

    someNumbers is the datasource, and number is the range variable. The datasource must implement IEnumerable, or IEnumerable. In this example, someNumbers implements IEnumerable. The compiler gives the range variable a type to match the type enumerated by the datasource. In the example above, number will be an integer. In general, the range variable is of type T, whenever the datasource implements IEnumerable. In the case where the range variable implements IEnumerable, the range variable is a System.Object. You never need to declare the type of the range variable; the compiler infers it. Consider you have the following string array
    string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };
    Task is to take the name which has length greater than 5 and make them capital letter. if we go in normal way we will put a for loop and then call it and assign a temp variable fetch it out hmmm lot of steps lets see how LINQ works on this

    LINQ syntax
    var query = from s in names where s.Length == 5 select s.ToUpper();

    simple rite it looks like T-SQL but in the reverse way don't confused. in T-SQL you know that you are going to query either a table or view but in LINQ you are going to query a object so you dont know from where you are going to query to from comes first instead of select