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

Thursday, February 18, 2010

Installing Visual Studio Extensions for Windows SharePoint Services on 64-bit Machines

As many of you may already know, the Visual Studion Extensions for Windows SharePoint Services makes developing custom SharePoint components easier. As some of you have discovered, you can't install on a 64-bit machine...until now.

Harold van de Kamp wrote a nice post in his blog on how to configure the original MSI installer to install on 64-bit machines using Microsoft's Orca tool. If you don't have time to experiment, he provides a link to a version that has already been configured to support installing on a 64-bit machine.



Directions for configuring the MSI for 64-bit support (includes link to download a version that installs on a 64-bit machine)

Monday, October 26, 2009

Adding a SlideShow to a SharePoint Site Redux

One of my most popular posts on this blog was a tutorial on adding a slide show to a SharePoint site using HTML, JavaScript and the Content Editor Web Part. If you haven't had a chance to view that tutorial, you can view it at this link: Add a SlideShow to the SharePoint Site using HTML, JavaScript, and the CEWP.

Just as it was one of my most popular tutorials, it was the one that seemed to cause trouble for some of the readers. Two of the most popular questions that I received were:

  • Why am I only getting the first image?

  • Why am I getting red X's?



I created this separate post to address troubleshooting the application.

Check Your Browser Version
Common Symptom: The first picture displays but the slide show doesn't play

To give you more information on the tutorial, I tested the tutorial on the following browsers:

  • Internet Explorer 6

  • Internet Explorer 7

  • Firefox 3.1x

  • Firefox 3.5x

  • Opera 0.9x


I've noticed that in some of the browsers, the comments in the JavaScript caused it to only show the first picture only. Try removing the comments from the JavaScript.


If you're not using the browsers that I've used to test the code, test the code in a stand-alone "test" web page to see if the browser supports the JavaScript. If you do that, make sure that you comment out the _spBodyOnLoadFunctionNames.push("runSlideShow"); line. If the code is not working, it's possible that your browser will not support the JavaScript code.


Also check the user's browser settings. The browser settings may be configured to not run JavaScript. If that's the case, then the script won't run.


Check Your Code

Common Symptom: The first picture displays but the slide show doesn't play; red X's appear instead of images

When I was assisting people with the slide show, I found two common problems:


  • Coders forgot to add the _spBodyOnLoadFunctionNames.push("runSlideShow"); statement before the ending </script> tag

  • Coders were not referencing the correct location for the images


Make sure that the _spBodyOnLoadFunctionNames.push("runSlideShow"); line appears before the closing </script> tag.

If the _spBodyOnLoadFunctionNames.push("runSlideShow"); statement is there, check to make sure that your path to reference the pictures is exactly right (the src property in the <img> tag). If you are having trouble with using the relative path, try referencing the absolute path (ex: http://www.bogus.com/images/image.jpg). If you are still getting the red X, it's possible that the image does not exist where you are referencing.


Check to Make Sure That Other Scripts on the Page Aren't Failing

Common Symptom: The first picture displays but the slide show doesn't play

If the page on the SharePoint site is running other JavaScripts (especially JavaScript that gets added through a feature) and one of those JavaScripts fail before the page gets a chance to load the slide show script, the slide show script won't run. For example, in my environment, we have a feature that adds JavaScript to change the logo. If that JavaScript fails, the slide show stops working. If your slide show is not working, view the errors on your browser to see if there is any JavaScript that's failing.


If You Are Referencing a Document Image Library for the Images, Check to Make Sure That You Have Read Permission on the Library

Common Symptom: The script "hangs", then red Xs appear when the user stops the script from running.

One of the things that readers reported was that their script would "hang", and when the stop the "hanging", it would show a red X. If you are referencing a document library for the images, you will possibly get "hanging" if the viewer does not have read permission to the document library. Either grant the viewers of the site at least read permission to the document image library, or move the pictures to a document image library where the users have read permission.


Hopefully these techniques are helpful. If you have other ideas to help fellow readers who are having trouble with the script, please post your suggestions.

Thursday, August 20, 2009

Links to FREE SharePoint 2007 Training and Reference Resources


Before I begin my article, I wanted to address a comment that was left on one of my posts regarding moderating comments. I didn't publish the comment because a) it contained language that was not safe for work or school, and b) it was not in a tone that would promote a healthy learning environment. I still want to answer the question anyway.

There are a few reasons why I moderate my comments:

  • Since a good bit of my audience are students, some of them try to be funny. If it's something clean, I'll post it. However, there are some who like to post inappropriate links or messages that are not safe for work or school. I don't want my blog to get flagged by web site traffic blockers because somebody finds it amusing to do a rickroll to pornographic material in my comments.

  • A pet hate of mine is when someone likes to use the comments to post spam. By moderating the comments, I can keep the spam off my site.

  • Although my blog is read by IT professionals of all levels, my blog is targeted to IT students and professionals who are starting out in a technology. As an adjunct professor, I believe in promoting a healthy learning environment where people can freely ask questions without being told that they're "stupid" or that they should just "Google it before asking it", and people can receive constructive criticism instead of negative attacks. By moderating the comments, I can block the people who are attacking others that have left comments or questions.





When I talk about job roles throughout the article, I mean the following in relation to SharePoint:

  • Administrator

  • Programmer/Developer

  • End User

  • (Web) Designer




I'm always looking for resources to help me with my day-to-day activities and my project work in relation to SharePoint 2007. Because of the economy, I need to use resources that are as inexpensive as possible. There's nothing less expensive than free.

Below are some free resources that I personally use and endorse.

The MSDN Windows SharePoint Services Developer Center is a fantastic source for all job roles with SharePoint 2007. After all, SharePoint 2007 is a Microsoft product. This site contains whitepapers, tips, and videos on all things SharePoint.

If you are looking for general tips and techniques, the SharePoint News Section of SearchWinIT.com contains great tips and techniques to help you with the various job roles in SharePoint. Note: you may have to register on the site to gain access to some of the information. Registration is free.

You can also access the free SharePoint E-Zine to get monthly information about the management, implementation, and governance of SharePoint. For example, the March 2009 issue contains traps to avoid to save yourself aggravation while supporting SharePoint, and the February 2009 issue talks about migrating from WSS 3.0 to MOSS 2007.

If you are a designer, look no further than Heather Solomon's Site. If you already work with branding and designing SharePoint sites, you probably already know that Ms. Solomon is considered the expert in SharePoint branding. Her site is the most comprehensive site on SharePoint branding.

If you are a developer, there are two helpful sites for you. Kirk Evans Links to Free SharePoint Developer Resources contains reference links that SharePoint developers need to have handy when working on development projects for SharePoint, and Point8020 free developer training is a 12-part course on writing code for SharePoint. It includes a module on Silverlight, which is what I'm learning now. ;)

When it comes to free SharePoint resources, a "new kid on the block" is Shelby Consulting. Bob Shelby is an IT professional working toward Microsoft Partner status in SharePoint, and he was kind enough to publish materials that he acquired and developed during the process. Most of the tutorials are targeted at administrators, but he does have a section for designers.

If you have any links to great free resources (in SharePoint) that you would like to share, please leave a comment and I will publish them.

Wednesday, June 3, 2009

A Look at CAML for SharePoint Development

Overview


For a quick summary of how CAML improved performance on a list items query, read this article from author and SharePoint development expert Sahil Malik.
The Collaborative Application Markup Language (CAML) is an XML-based language that is primarily used to perform data manipulation. In a number of cases, using CAML helps improve your program's performance.

CAML Tutorial


If you are unfamiliar with CAML syntax, Karine Bosch wrote a fantastic tutorial on writing CAML queries for SharePoint Magazine. She covers all the basics and the "gotchas" of CAML syntax in an easy-to-follow manner.

Helpful Tools


When I am writing code where I need to use CAML, I am partial to two FREE tools.
Screenshot of Stramit CAML viewer. Click on the image for a larger view.


The Stramit CAML Viewer is an "all-in-one" tool that not only lets you view and test CAML queries, but it also displays the GUIDs for lists and views. You can download this tool from CodePlex.

Screenshot of U2U Caml Query Builder. Click on the image for a larger view.


The U2U CAML Query Builder is a slick, time-saving tool that will automatically build the CAML query for you based on the information entered by you. It also has a feature where you can copy the built CAML query to the clipboard. You can download this tool from the U2U site.

CAML in Action: A Simple Query



Note: This code will only work on a server where SharePoint is installed. For example, if you are going to add a view to a list that exists on http://bogus/bogussite, you need to have this code on the server that houses http://bogus.


In this example, I have a list on my site called Doctor Who Episodes. I would like to get the list of episodes that starred Jon Pertwee as the Doctor, and I would like to order that list by the year. My application, written for demonstration purposes only (no "fancy" formatting, application usability design, or full performance evaluation was considered), will display the episode list in a text box the moment the application runs (Form_Load).
Click on the image for a larger view.


After creating a new C# Windows application called SimpleCAMLQuery, I added reference to the Microsoft.SharePoint library in my project and referenced the library in my code.

using Microsoft.SharePoint;


I added the following in the Form_Load event:
I defined three variables to hold the URL for the site, the list name, and the view name.

String siteName = "http://bogus/bogussite";
String listName = "Doctor Who Episodes";
String viewName = "All Items";


I then used SPSite, SPWeb, and SPList to open my list on my site. I also used SPQuery to open my view, since this class has the method/function to use for the CAML query.

SPSite site = new SPSite(siteName);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[listName];
SPQuery query = new SPQuery(list.Views[viewName]);


I then build my CAML query using the StringBuilder class. You can also put the CAML query directly into a string object if you prefer. If the field has spaces or special characters in the name (as it does in my example - the field in my list is called "The Doctor"), you need to reference the hexadecimal values in the name.


System.Text.StringBuilder oSb = new System.Text.StringBuilder();
oSb.Append(" <Where>");
oSb.Append(" <Eq>");
oSb.Append(" <FieldRef Name=\"The_x0020_Doctor\" />");
oSb.Append(" <Value Type=\"Text\>Jon Pertwee</Value>");
oSb.Append(" </Eq>");
oSb.Append(" </Where>");
oSb.Append(" <OrderBy>");
oSb.Append(" <FieldRef Name=\"Year\" Ascending=\"True\" />");
oSb.Append(" </OrderBy>");

Note: The order in which the Where and the OrderBy tag blocks appear in the CAML doesn't matter. Some people place the OrderBy tag blocks before the Where tag blocks. I usually place the OrderBy tag blocks after the Where tag blocks because I'm used to writing SQL, and in SQL syntax, the Order clause comes after the Where clause.

I then called the Query method/function of the SPQuery object to set up the CAML query

query.Query = oSb.ToString();


Finally, I put the results in a SPListItemCollection object and looped through the object.

SPListItemCollection results = list.GetItems(query);
foreach (SPListItem i in results)
{
  txtResults.Text += i["LinkTitle"].ToString();
  txtResults.Text += Environment.NewLine;
  txtResults.Text += i["Synopsis"].ToString();
  txtResults.Text += Environment.NewLine;
  txtResults.Text += i["Year"].ToString();
  txtResults.Text += "----------------------------------------";
  txtResults.Text += Environment.NewLine;
  txtResults.Text += Environment.NewLine;
}



Here is the full code:
Click on the image for a larger view.


Additional examples of CAML in Action


If you would like to see additional examples of using CAML, view these previous posts on my blog:


If you have any questions, comments, or additional examples, please feel free to post a comment.

Friday, May 8, 2009

SharePoint Development Tutorial: Programmatically Create View on a List

While it is easy to create a view for a list in SharePoint, you may find a scenario where you need an easier way to create a view on a list. For example, I had a situation where I had to create over 30 views for one list. Creating the views using SharePoint can be time-consuming, and it could increase the possibility of user error because of the manual process. Using C#, the Microsoft.SharePoint library, and CAML, one can create a view for a list on SharePoint.

This illustration will demonstrate the simplest example of using C# code to add a view to a list (as a console application). You may want to make modifications for your needs, or use it as a foundation for a larger-scale application.


Note: This code will only work on a server where SharePoint is installed. For example, if you are going to add a view to a list that exists on http://bogus/bogussite, you need to have this code on the server that houses http://bogus.



The Scenario


In this illustration, there is a list on a SharePoint site called "Doctor Who Episodes". This list needs a new view called "JNT Era", which will display Doctor Who episodes made during the JNT era (1980-1989). This list has four fields:

  • Title

  • The Doctor

  • Synopsis

  • Year



See the example list

After starting a new project, you will need to add a reference to the Microsoft.SharePoint library in your code, and you will need to indicate that you will be using the library in your code.
See the example snippet

The next step is to define the SharePoint classes that you will need to perform the action. The four classes that you will need are:

  • SPSite - Collection of sites

  • SPWeb - The SharePoint web site

  • SPList - A List on a SharePoint web site

  • SPViewCollection - Collections of views on a list



In the code, you will need to access the website. You will define an instance of the SPSite class to get the site, then you will need to define an instance of the SPWeb class to have a reference to the actual site.

SPSite oSite = new SPSite("http://bogus/bogussite");
SPWeb oWeb = oSite.OpenWeb();


Alternative to SPList oList = oWeb.Lists["List Name"]

You can also access the list by the GUID. Use the following logic if you want to access the list by the GUID rather than the name:

Guid gui = new Guid("The List GUID");
SPList oList = oWeb.Lists[gui];


Then, you will need to access the list that will get the new view, and you will need to access the list's collection of views.

SPList oList = oWeb.Lists["Doctor Who Episodes"];
SPViewCollection oViewCollection = oList.Views;


Finally, you will need to define the new view name.

string strViewName = "JNT Era";

See the example snippet

We are ready to define the fields that will be displayed in this new view. In this example, the view will display all the fields. You will need to define an instance of the System.Collections.Specialized.StringCollection class to hold the names of the fields that will be displayed in the view.

System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();

To add the fields, use the Add() function of the class. You do not have to use the special hexadecimal characters to represent spaces and special characters for the field names. You can use the literal field name.
See the example snippet


Helpful Links:

CAML Syntax

Stramit Caml Viewer

We have to define the criteria for the view using CAML. In our illustration, we want to display the Doctor Who episodes from 1980 until 1989, and we want to display them in ascending order. Your scenario may be different, so your CAML will be different. If you are unsure of CAML syntax, a link to a site that has documentation on the CAML syntax has been provided for you on this post. If you want to make sure that your query will work before you begin coding, you can use the Stramit Caml Viewer to test your CAML query. A link to the tool has also been provided for you on this post. In this illustration, I am using the StringBuilder class to hold the CAML query. Then, I am converting it to a String.
See the example snippet

Finally, we are ready to add the view. The Add() function of the SPViewCollection class adds a view to the list. The Add function
takes six parameters:

  • View Name - a string

  • Collection of View Fields - string collection

  • CAML query - a string

  • Row Count - an integer

  • Is this paged? - a boolean

  • Is this going to be the default view? - a boolean


You also have to call the Update() function of the SPWeb class to make sure the changes "take".

oViewCollection.Add(strViewName, viewFields, query, 5000, true, false);
oWeb.Update();

See the example snippet

After completing, compiling and running the program, the list now has a new view!
See the results

If you would like a copy of the skeleton code, you can download the RTF file of the skeleton code here.

Please post your questions or comments, and I will answer your questions to the best of my ability.

Wednesday, April 22, 2009

SharePoint Development Tutorial: Programmatically Deleting All Items from a List

Microsoft Reference Documentation: Microsoft.SharePoint namespace

Sometimes you need a quick way to delete all items from a SharePoint list. You can manually delete each item from the list without writing code, but this can be time-consuming, especially if you have a high quantity of items on the list.


Click the link to download the source code for this post

Licensing and Warranty

You may use the code as you wish - it may be used in commercial or other applications, and it may be redistributed and modified. The code is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. By downloading the code, you agree to defend, indemnify, and hold harmless the Author and the Publisher from and against any claims, suits, losses, damages, liabilities, costs, and expenses (including reasonable legal or attorneys' fees) resulting from or relating to any use of the code by you.




You can write code to programmatically remote items from a SharePoint list using the classes in the Microsoft.SharePoint library (namespace). There are two ways that you can delete all the items from the SharePoint list programmatically:

  • Using the DeleteItemById() function of the SPList class

  • Recommended for performance:Building CAML and using the ProcessBatchData() function of the SPSite class



I will demonstrate both approaches. The skill sets required for these approaches are:

  • C# knowledge

  • A general knowledge of the classes in the Microsoft.SharePoint library/namespace

  • A general knowledge of CAML (for approach #2)



Note: This code will only work on a machine on which SharePoint is installed.


Approach #1: Using the DeleteItemById() function of the SPList class


The function DeleteItemById() of the SPList class expects a numeric parameter ID, which is the ID of the item to delete. This code snippet, which is a slightly modified version of a function in Keith Richie's SPPurgeList (http://www.codeplex.com/sppurgelist), demonstrates how to delete all items by capturing the IDs in a hashtable, reading the hashtable, and calling the DeleteItemById() function. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.

Sample: Using DeleteItemById() [Opens In Another Window]

The issue with Approach #1 is performance. While it is negligible on a list with a small amount of item, it becomes more noticeable on a list with hundreds of items. Each call to DeleteItemById() takes about 1 second. If you have hundreds of items, this could take a while to run.

Approach #2: Building CAML and using the ProcessBatchData() function of the SPSite class


Passing CAML to the ProcessBatchData() function of the SPWeb class runs faster than caling the DeleteItemById(). The following code snippet demonstrates how to build the CAML (using the StringBuilder) and pass the CAML to the ProcessBatchData() function to delete the items. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.

Sample: Using CAML and ProcessBatchData() [Opens In Another Window]

A Note About Using CAML to Delete Items from a Document Library

The code snippet above will work for the lists except for document libraries. If you try to run the code snippet on a document library, you will get an error about file names. The document libraries require an additional parameter called owsfileref. The following link to the code snippet demonstrates how to build the CAML for document libraries.
Sample: CAML for document libraries [Opens In Another Window]

If you have any questions or comments, please feel free to post them, and I will answer the questions to the best of my ability.

Tuesday, March 24, 2009

Add a SlideShow to the SharePoint Site using HTML, JavaScript and the CEWP

While the SharePoint OOTB web parts for displaying images are nice, the functionality of the web parts are very limited. Particularly, the OOTB web parts do not have a true "slide show" capabiilties that many end users are looking for. You can write your own custom web part to display images like a slide
show. However, this solution will not only take time and effort to do, but if you are just a “power user” for your SharePoint site, you may not have the right tools and permissions to make your own web parts. If you are familiar with JavaScript and HTML, you can add your own slide show on your site.

For a step-by-step guide that you can download: Add a Slideshow on a SharePoint Site using Javascript, HTML, and the Content Editor Web Part

Here is a video demonstrating how to add the slideshow:


UPDATE: If you are having trouble with getting this to work, see the blog post from 10/26/2009 for troubleshooting tips.

Thursday, January 8, 2009

SharePoint Tutorial: Creating a Custom List Form for a SharePoint List

SharePoint has built-in forms for various actions on a list.

  • There is a form for displaying a selected item

  • There is a form for adding a new item

  • There is a form for editing an item



While the built-in forms are sufficient for the tasks at hand, you may run into a situation where the built-in forms do not satisfy your needs. For example: I had to recently create a list on a site collection. My business user wanted the other users to be able to enter their information in the form when adding it to the list. However, when they modify the record, the user should not be allowed to modify certain fields after they entered the information.

With SharePoint Designer 2007, you can create a custom form for any of the actions (display, add, edit) and attach it to the list.

I've created a document which illustrates how to create a custom list for a SharePoint list and attach the custom list to a list action.

I've also created a video illustrating how to customize the edit action.

If you have any questions, please post a comment or send me an e-mail, and I will answer your question to the best of my ability. You can get my e-mail address by viewing my Blogger profile.

Monday, December 29, 2008

SharePoint Tutorial: Connecting SQL Reporting Services to a SharePoint List

Sometimes you may need to create reports on the SharePoint list data. For example, you may need to create a report for your manager on surveys entered by your users. You can use SQL Reporting Services to connect to a SharePoint list and develop a report from that data.

I've created a tutorial and a video on how to do this.

If you have any questions, please post a comment and I will try to answer to the best of my ability.



Thursday, December 18, 2008

C# and SharePoint 2007 Tutorial - Accessing the SharePoint Data Using Web Services (Part 3)

This is part 3 of the series. If you haven't read parts 1 and 2, please click on the following links to read:


I have published part 3 of the series. The tutorial can be accessed from the link at the end of this post.

If you have any questions, please post a comment and I will answer to the best of my ability. :)



Enjoy!

Tuesday, December 9, 2008

C# and SharePoint 2007 Tutorial - Accessing the SharePoint Data Using Web Services (Part 2)

This is part 2 of the series. If you haven't read part 1, please read it here.

I have published part 2 of the series. The tutorial can be accessed from the link at the end of this post.

If you have any questions, please post a comment and I will answer to the best of my ability. :)



Click to read Part 3 of the tutorial
Enjoy!

Monday, December 8, 2008

Workaround for Installing SharePoint Extensions for Visual Studio on Windows XP


Note: Part II of the SharePoint "Behind the Scenes" Tutorial will be posted tomorrow.


The SharePoint Extensions for Visual Studio (versions 2005 and 2008) make SharePoint development so much easier. However, it assumes that you are developing on a machine with Windows Server 2003, which has the SharePoint Server components already installed.

What if you develop on a Windows XP or Windows Vista machine, and you port the code to a SharePoint server? Unfortunately, you cannot install the SharePoint Extensions on a Windows XP or Windows Vista machine out of the box.

However, with a small registry hack, you will be able to install SharePoint Extensions for Visual Studio on a Windows XP or Windows Vista machine. I did the registry hack on my XP machine, and I was able to install the extensions.

Warning: Approach registry hacks with caution. Make sure you make an image of your machine or a backup of your registry before doing the hack in case something goes wrong.

If you would like to see how it's done, click here to see the video demonstration (WMV file)



Source: Janne Matilla's Blog

Wednesday, December 3, 2008

C# and SharePoint 2007 Tutorial - Accessing the SharePoint Data Using Web Services (Part 1)

As I promised, SharePoint developers, I wrote a tutorial on using a web reference to access SharePoint data using a C# program. This is very useful if you don't want to run your program on the same server where SharePoint 2007 is installed. The tutorial can be accessed from the link at the bottom of this post.

If you have any questions, please post a comment and I will answer to the best of my ability. :)



Click to read Part 2 of the tutorial
Click to read Part 3 of the tutorial

Enjoy!

Wednesday, November 26, 2008

How To Add Third-Party Widgets to Your SharePoint Site



Digg!


Here is an example of a SharePoint site using third-party widgets. The widgets on the right are the Google Map and the Yahoo! Map widgets. The one in the center (left) is one that plays the Peanut Butter Jelly Time video.


Web widgets are useful pieces of code that will allow you to "jazz up" your site without having to compile or use your own code. For example, on one of the sites at work that I had to develop, I'm using the Mapquest widget to allow the users to easily get driving directions. Yahoo! and Google also have some useful widgets that you can use as well.

You can easily embed the widgets on your web page, social networking page, or blog, and by using the Content Editor Web Part, you can use these widgets on your SharePoint 2007 site as well.

I've recently published (on Scribd) step-by-step instructions on how to add third-party widgets to your SharePoint site. You can click on the link below for the directions.



Enjoy!

P.S. to the developers who are reading this: You're probably champing at the bit to write some code to interact with SharePoint. I haven't forgotten about you! I will be featuring more examples of interacting with SharePoint using code over the next few weeks.

Tuesday, November 25, 2008

How to Copy a List on SharePoint 2007 From One Site to Another Site Using Existing SharePoint Functionality


Digg!


If you work in SharePoint 2007, you may have to copy or move a list from one site to another site for one reason or another. For example, you may have to move a list from one site to another, or a site administrator likes the looks of how a list was created on another site and s/he would like to have that list for his or her own data without having to recreate the list manually.

While you can copy or move the lists programmatically, it may take some time to do. Using SharePoint's existing functionality, you can copy or move a list from one site to another site without having to write a line of code.

I've recently published (on Scribd) step-by-step instructions on how to copy a list using SharePoint's existing functionality. You can click on the link below for the directions.



Enjoy!

Wednesday, November 19, 2008

SharePoint Development - The Essential Tool Set


Digg!



Please make sure that you install the SharePoint Extensions for your Visual Studio environment.
Extensions for Visual Studio 2005
Extensions for Visual Studio 2008

For a reference to the SharePoint Class Library: MSDN on Microsoft.SharePoint


People who know how to develop in .NET (particularly C# and ASP.NET) already know the foundations for developing for SharePoint 2007. All you need to know is the classes that SharePoint uses, how to access data on SharePoint through your code using GUIDs, and if you are also the SharePoint administrator, how to configure SharePoint to use the custom code by referencing the assembly information.

So how the @#&$% does the developer get the GUIDs and the assembly information?


Note
Before beginning your development project, check out the CodePlex site. CodePlex is Microsoft's open source community. There are numerous tools on this site available for your use, and someone may have already written a tool or web part that you may have to develop.


There are a number of tools that I use to assist me with my SharePoint development. All of these tools that I use are not only free to use, but the majority of the tools are endorsed by Microsoft (the manufacturers of SharePoint and .NET).


  • Stramit Caml Viewer
    Site for Caml Viewer
    Written by Renauld Comte, this robust tool not only gives you information about list and view GUIDs, but it also generates CAML queries for you. So if your application is using CAML queries to get the information, this tool saves you a boatload of time because it not only generates the XML for the CAML query, it also generates the StringBuilder logic for creating the XML for the CAML query!

  • A Simple GUID Picker
    Site for the GUI Picker
    Written by Ronalus, this tool allows you to get the GUIDs for the lists and views on a SharePoint site. This tool is simple yet useful. I use this tool when I'm doing reporting using SharePoint data and SQL Reporting Services since you need to know the list and view GUIDs to connect to the SharePoint "data source".

  • Reflector
    Site for Reflector tool
    Originally written by Lutz Roeder and maintained by Red Gate, this tool is essential for development. This tool returns the assembly information for a class library (dll). All you have to do is drag and drop the dll into the tool, and it will return the information for you, including the assembly information (which is needed for configuring the SharePoint server to use your custom web part or program) and the classes that are part of the library.

    If you have Windows Media player, you can see it in action in this video.

  • Warmup Server
    Download Warmup Server
    Ideally, you want to create a virtual environment in order to do your SharePoint development. If you develop in a virtual environment, then this is the tool for you. This tool allows you to create a MOSS 2007 VPC (Virtual PC) image that you can use. Here is the tutorial on using this tool. I haven't tried it since in my environment, we actually have physical machines that we work from, although I am leaning toward setting up virtual environments for my development.

  • SharePoint Spy
    Site for SharePoint Spy
    Written by Echo Technologies, this free tool will allow you to look at the internal data on a SharePoint site and compare the effects of making a change without changing the actual data. This tool is really helpful if you are writing a program that is manipulating data on a SharePoint list, and you don't want to have to change the data each time. You can also use this tool to compare settings between sites, views and lists to help troubleshoot configuration problems.

Monday, October 27, 2008

C# and SharePoint 2007 Tutorial - Getting the Total Responses for All the Surveys on a SharePoint Site


Click the link to download the source code for this post

Licensing and Warranty

You may use the code as you wish - it may be used in commercial or other applications, and it may be redistributed and modified. The code is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. By downloading the code, you agree to defend, indemnify, and hold harmless the Author and the Publisher from and against any claims, suits, losses, damages, liabilities, costs, and expenses (including reasonable legal or attorneys' fees) resulting from or relating to any use of the code by you.




A project that I'm currently working on for my CIO is to create more robust reports from surveys created in SharePoint 2007. I'm using a SQL Report Server Project to build these reports from the data entered on a SharePoint survey. In order to be able to create a report, the SharePoint survey needs to have responses. I really didn't want to spend time manually checking each survey to see whether it has responses. I wrote a program using C# to automatically check the surveys on the SharePoint site and return the item counts (responses) to me via e-mail.

Overview


The program uses the Microsoft.SharePoint library to access the SharePoint site, and it uses the System.Net.Mail library to send the e-mail. To keep the program flexible, I keep the "to e-mail", "from e-mail", SharePoint site URL, and SMTP server name in the app.config file. That way, if there's a change to any addresses, I can just change the config file instead of having to change and to recompile any code.

In a nutshell, the program does the following:

  • It gets the values from the app.config file

  • It opens the SharePoint site

  • It loops through the Lists collection of the site and returns the item count (responses)

  • Sends the results to the e-mail address referenced in the "to e-mail" element of app.config



Highlights


Traversing through the Lists collection on the SharePoint site
Starting from line 25 in the source (Program.cs), it uses the SPSite class and the SPWeb class (Microsoft.SharePoint) to open the site (as referenced in app.config). Using a foreach loop, it looks at each list in the collection and returns the value in the Items property of the list, which is the total number of responses for the survey. Here is the code snippet:


using (SPSite site = new SPSite(strURL))
{
  using (SPWeb web = site.OpenWeb())
  {
    foreach (SPList list in web.Lists)
    {
      msgSB.Append(list.Title);
      msgSB.Append(" - ");
      SPListItemCollection items = list.Items;
      msgSB.Append("Total items: " + items.Count);
      msgSB.Append("\r\n");
      msgSB.Append("\r\n");
    }
  }
}


Note that msgSB is an instance of the StringBuilder class, and I am recording all the results in this instance.


If your SMTP server requires a specific ID and password for authentication, you can use the System.Net.NetworkCredential class to specify the ID and password. Here is a code snippet on how to set up your SmtpClient instance to use the specific credentials:


System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ID, password, domain);
smtpClient.Credentials = credentials;


If you are going to use this method, please be aware of some security precautions, such as not storing the ID, password, and domain in clear view of the naked eye. For example, if you want to store this information in the app.config file: encrypt the values, put the encrypted values in the app.config file, and use the code to decrypt the values.

Sending an E-Mail
The MailMessage class sends the e-mail. However, I wouldn't be able to send an e-mail without referencing an SMTP server. The SmtpClient class is used to "set up" the SMTP server. Below is the code snippet, found on line 42 in the source.


String message = msgSB.ToString();
MailMessage mail = new MailMessage(fromEmail, toEmail, subject, message);
String mailServer = Properties.Settings.Default.mailServer;
SmtpClient emailClient = new SmtpClient(mailServer);
emailClient.UseDefaultCredentials = true;
emailClient.Send(mail);


Note that I'm converting the StringBuilder instance (which contains the results) into a string. Also note that the UseDefaultCredentials property is set to true, which means that the program will use the current logged in user's credentials to authenticate against the SMTP server. One of the ways that network administrators prevent hackers from hijacking their SMTP server to use for sending spoof e-mails is they configure the SMTP server to require authentication.

The app.config file


Since it's possible that addresses can change due to machine upgrades and domain name changes, I stored the following four values in the app.config file:

  • <toEmailAddress> - The e-mail address of the person who should receive the report

  • <fromEmailAddress&; - The e-mail address of the person of the "sender". This is typically a "webmaster" address or a generic "SharePoint administrator's" e-mail address

  • <surveyURL> - The ROOT URL where the surveys are located. For example, if your surveys are on site http://bogussite/surveys, enter http://bogussite/surveys.

  • <mailServer> - The name of the SMTP server



In the source code, I wrote an explanation of what values should be in each element. You will need to replace that information with the actual values.
Here is an example configuration snippet of the app.config file:


<SurveyListCount.Properties.Settings>
  <setting name="toEmailAddr" serializeAs="String">
    <value>jennifer.lewis@mymwalimu.org</value>
  </setting>
  <setting name="fromEmailAddr" serializeAs="String">
    <value>webmaster@bogus.com</value>
  </setting>
  <setting name="surveyURL" serializeAs="String">
     <value>http://SomeSharePointSite/SurveysSubsite</value>
  </setting>
  <setting name="mailServer" serializeAs="String">
    <value>SMTP.BogusSite.com</value>
  </setting>
</SurveyListCount.Properties.Settings>




Before Using the Code for Your Own Solution:

  • The compiled program (binary) must reside on the same machine where SharePoint 2007 is installed, or you will get errors.

  • Ideally, you want to have your surveys in a separate subsite of your site collection. If you have other lists in your site collection or subsites, the program will pick up the item counts of those lists as well.

  • If you want the program to run at a certain time (ex: every Wednesday evening), you will need to use Windows Scheduler to schedule the binary to run automatically.


Monday, October 6, 2008

Wicked Cool Sites for SharePoint Development Help

SharePoint development is not as bad as I thought it would be, but finding information about SharePoint development is pretty difficult, in my opinion. I don't mean using SharePoint Designer for customizing pages or creating custom workflows. I mean using C# or VB.NET to interact with SharePoint.

I found a few sites that are really helpful:

  • Microsoft's Home Page for SharePoint Developers - This site contains webcasts, whitepapers, and tutorials on using .NET with SharePoint. It also has a Virtual Lab to try the exercises. (Note: I didn't try the Virtual Lab)

  • SharePoint for Developers - this series written by Microsoft MVP Gunnar Peipman talks about using .NET to interact with SharePoint. While the series is not complete yet, there is enough information to get one's feet wet, so to speak.



CodeProject also has a lot of code examples of using .NET with SharePoint. If you want to download the examples, you will need to register for the site, but registration is free.

If you don't have time to develop a solution, CodePlex has a lot of open source solutions for SharePoint.

Even though this site focuses more on using SharePoint Designer for site customization, I think it's worth a mention:

  • SharePoint for Developers - These are webcasts sponsored by Microsoft that give fantastic information on how to customize the SharePoint site with SharePoint Designer.