Owner: Gunnar Peipman's ASP.NET blog URL:http://weblogs.asp.net/gunnarpeipman/ Join Date: Sun, 13 Jan 2008 10:14:25 -0600 Rating:0 Site Description: ASP.NET WCF WF .NET C# SharePoint Site statistics:Click here
Some cool SQL Server user names 2008-02-16 14:17:00 One day when I checked SQL
Server messages in Event Viewer I found a long series of unsuccessfull attacks. Somebody desperately tried to guess user names and passwords. The favourite ones were the following user names:
sa
sql
admin
administrator
administrador
root
access
db
database
datenbank
saadmin
web
webadmin
master
chef
server
So try not to use these user names and - if possible - don't make your SQL Server
visible outside.
C# and question marks 2008-01-05 11:53:00 One cool operators that C# offers us is ?? But before ?? we should know what does ? after variable type name. So, let's take both of these question marks and let's see what they are. Also let's jump for a while behind compilator to see IL code that compiler produces.
Let's start with single question mark. Single quetsion mark after variable type tells to compilator that this variable is Nullable<T>. To see if my statement holds true let's look at the following code.
class Program{ static void Main(string[] args) { Int32? x; Nullable<Int32> y; }}
There are two lines of code in method Main(). First of them declares Int32 type variable using question mark. Second line declares Nullable
SharePoint: How to display blog feed using XML Web Part? 2008-01-02 10:09:00 I wanted to show our company's blog feed on our intranet first page. There some empty space I wanted to fill somehow. I found a good solution, so there was no need for some third-party Web Parts. Also there was no need to write any additional code.
Here's my blog feed example as easy step-by-step guide.
Move to SharePoint page you want to add your blog feed.
Open this page in edit view and add new Web Part called XML
Web Part.
If Web Part is added to page then open it's settings window.
On the field XML Link insert your blog feed URL. Check out if link is correct and content is receivable by clicking the link titled as Test Link.
Push button titled as [XSL Editor].
XSL editing window is opened and now insert XSL code given below. Whe
Extension methods - how they look like after compiling 2007-12-29 04:30:00 This blog entry is supposed to be the continuation for entry C# Extension
Methods where I told what extension methods are and how to use them. This time I will tell about what they are behind the curtains. Do they really add new methods to current classes or not? I mean, are these methods really added to classes or are they something else?
Let's start with extension that adds Nl2Br() method to string. This function is well-known from PHP programming language. So, here's the code.
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;namespace csharptest{ public static class Ext { public static string Nl2Br(this string s) { &nb
How to connect Business Data Catalog (BDC) lists to other SharePoint lists 2007-11-29 10:33:00 Recently I had to create Web Part connection between BDC based list and usual SharePoint list. BDC list was provider and usual SharePoint list was consumer. I was pretty surprised if found out that I was able to connect BDC based list only to other BDC based Web Parts. All the other Web Parts were not able to be consumers for them. Same time BDC lists were able to be consumers of the other web parts.
This time Google wasn't my help and documentation. Also MSDN wasn't able to help me - there were documents for everything but no texts or explanations about how and what I can use. Here is the example of perfect documentation. After hours of investigating and experimenting and hacking everything started working. As a result I created a web part that re Read more:Business
C# Extension Methods 2007-11-17 17:30:00 One of new cool features that will be available in C# 3.0 are extension methods. Extension
methods will allow us to extend existing classes with new functionality. In this example I will show you how to extend System.string with two methods that are very popular in PHP: nl2br() and md5().
Extension methods must be defined as static methods of some static class. Now let's write the static class called StringExtensions.
using System;using System.Security.Cryptography;using System.Text;namespace MyExamples{ static class StringExtensions { public static string Nl2Br(this string s) { return s.Replace("\r\n", "<br />").Replace("\n", "<br /> Read more:Methods
C# automatic properties 2007-11-15 16:37:00 C# 3.0 makes it very convenient to create properties that doesn't carry any functionalities besides returning value of attribute and assigning value to it. This new feature is called automatic
properties.
Suppose we have a class with big load of properties that just return attribute values and assign given values to them. Something like you can see in following code.
public class Product{ private string description; private Int32 size; private decimal price; public string Description { get { return description; } set { description = value; } } public Int32 Size { get { return size; } &nbs
Testing sorting algorithms 2007-10-28 07:57:00 Some time ago I had to deal with sorting algorithms. Besides my main task I found a good way how to test custom sorting algorithms. This blog entry is one of early birds, more about sorting algorihtms is coming soon. Hopefully some time after TechEd 2007 for Developers. The procedure I wrote to test sorting algorithms is simple and works. Of course, I am always opened for better ideas if somebody wants to suggest some. Here's the little overview about what I've done.
Is array sorted?
At first, let's look at method that checks if array is sorted or not. To make things simpler I expect that all members in array implement IComparable interface. This expectation has very strong point: it is easy to compare objects of classes that have comparison operators defined but it is Read more:Testing
How to create grayscale images on .NET 2007-10-22 16:14:00 In this example we will use simple color recalculation method to grayscale images. For each pixel on image we will calculate "average color" that we write back on image. Average color is sum of current color's red, green and blue channels integer value divided by three. This value is assigned to all three channels for current pixel.
Colored picture of rabbit commitingthe crime.
Although grayscaled she is still on action.
The cool thing about this rabbit is her colors - black, white and gray. As you can see rabbit colors are almost the same on both pictures. Okay, let's see the code.
public Bitmap GrayScale(Bitmap Bmp) { int rgb; Color c; for (int y = 0; y <Bmp.Height; y++) for (int x = 0; x Read more:create
SQL-DMO and searching from meta-data 2007-10-21 11:53:00 Lately I faced the need to search for specified strings in all stored procedures in given database. I found SQL
-DMO library very useful for it. So, ten minutes later I had a simple form that fits my needs perfectly. And some minutes later I had there some more cool features. As a result I had form that enables me to search for strings from stored procedures, user defined functions and views. Also I was able to search strings from mentioned objects names. I tested it on a little bit larger MS SQL Server database, iterating approximately through 500 database objects and searching
for given string and I mentioned no performance problems. So, here's the example.
Getting Started
SQLDMO is provided with MSDE and MS SQL Server. SQLDMO stands for SQL Data Management Objects. It is library th
MoreDefensiveDatasource 2007-10-17 03:08:00 At the end of the previous month I wrote a posting on DefensiveDatasource class that can be used to associate ASP.NET's GridView with collections containing objects of different types and based on one and the same base class. Dividing data into pages in GridView does, however, not succeed. An error message "The data source does not support server-side data paging" is displayed.
There is a simple solution to this problem. We have to create a new class conforming to the ICollection interface based on DefensiveDatasource. As we need to deal with DefensiveDatasource only in case of collections of the IList type, I restricted the collection selection of the new class so that only IList based collections can be submitted to the class as initial collections.
For more extensive collection support
Creating simple tasks calendar for SharePoint sidebar 2007-10-09 18:11:00 I needed view of SharePoint calendar where I can see not accomplished activities from beginning of calendar until tomorrow. If you ask me why I needed something like this, there is a simple answer: this calendar contains almost everything that is going on (mainly different milestones and deadlines). As it is common calendar and everybody wants to see everything then there is no need for personalized filter. And there was one more requirement - there must be support for states of tasks describing if event is planned, done or canceled, so we can show in sidebar calendar only the planned tasks or events.
Here is my solution to this problem.
Create new calendar if you don't have one.
Add new column called Begin Date.
Follow the instructions given in my previous post Fil Read more:Creating
Filtering SharePoint calendar by Start Time 2007-10-09 04:39:00 I had a problem when trying to filter WSS 3.0 calendar list by Start
Time column. This column doesn't appear in filtered columns list somewhy. I found solution that smells like dirty hack to me, but at least it solved my problem. Oooh, this SharePoint is sometimes so kinky.... I'm almost sur ethere is better way to do that but if you are in hurry and need a working hack then here we go!
So, let's go step by step:
Open Calendar list settings.
Add new column called (by example) Begin.
Choose "Calculated" as a type of new column.
In the Formula field insert the following formula:=[Start Time]
Select Date and Time as data type of new column.
Clear check from "Add to default view" checkbox.
Save the new column.
Now you should be able to use the column called Begin instead of&n Read more:Filtering
DefensiveDatasource 2007-10-07 16:01:00 We recently implemented some changes in the software development methodology used at Developers Team and quite by chance encountered an interesting .Net 2.0 problem. Namely, when an object array inherited from the same base class is cast on a base class and submitted to the data binder, the latter gets confused. The array contained objects from different classes but all these classes had one and the same base class.
After some investigation into the problem we realised the following sad fact. When data are bound to GridView, for example, then it is assumed that all objects have been created based on one and the same class. It means that in case of an array from an abstract class Animal, containing objects from classes Cat and Dog, we'll get an error. GridView takes the first obje
.Net and Deep Copy 2007-10-07 13:56:00 Some time ago I had to clone objects and .Net's shallow copy proved to be insufficient – it was necessary to use deep copy. No good tools are provided by .Net itself. If required, the object to be cloned must conform to the ICloneable interface and the Clone() method can be defined for the object. As the classes were not very numerous, but relatively bulky and complicated, there was no point in writing a Clone() method for all of them. I needed something else.
With the help of Google I found a very smart method for performing deep copy. Its performance may not be exceptional but it does its work nicely. The idea of the method is simple – first the object has to be serialised and then deserialised. Serialisation loses the relation between the specific instance of the object and the
Windows Installer and Extensibility.dll 2008-03-12 18:39:00 Windows Installer
is one of my favorite problem childs - of course, when it has mysteriuos problems. I found a problem when installing Outlook add-in on machine that has no extensibility features installed on it. Something was missing and installer gave me the famous "Unable to get installer types" error. After pointless waste of time tracing installer and checking its logs I checked out what's going on in file system.
By default, Extensibility.dll was not installed. When I added it to setup package manually it was copied to application folder as expected. But still nothing happened. I was even able to create some packages that installed without errors but add-in was not installed. Cool, ah?
Solution to this problem is simple. There is another version of Extensibility.dll that yo Read more:Windows
Is Automatic Property Same as Property? 2008-03-13 03:26:00 I was listening one session on TechEd (night after long party, yeah) and I was thinking about automatic properties - are they really exact equivalent to usual properties? Something made me suspicious, so I opened my laptop at first free moment and checked out what's going on.
At first let's write a little class. We will use it later to play with automatic properties.
public class AutoPropTest{ private string name;
public string Name { get { return name; } set { name = value; } } public string Name2 { get; set; }}
This class has one usual property (Name) and one automatic property (Name2). Now let's compile this class and let's see how th
How to Update SharePoint List Items without Creating New Versions 2008-03-22 19:14:00 Migrating data from SP2001 to MOSS2007 is fun - if you know what I mean. During migration we discovered that migration tool has some serious bugs. I wrote some code that fixes some problems after importing process. There I found a problem - I needed to update list items without getting new versions of them after updates. Usually we see samples like this in internet:
SPList list = web.Lists["myList"];SPListItem item = list.Items
[0];
item["myField"] = "my value";
item.Update
();list.Update();
This code works perfect for end-user scenarios. It creates automatically new version of list item as Update method is called. I needed no new versions and there fore this solution wasn't great help for me. I found list item's method SystemUpdate. The following code is same a Read more:Creating
How to bulletproof the loops 2008-04-04 12:29:00 Here is one example about how to bulletproof the loops. This example holds well for legacy code and - of course - for hurry-written-code (that might be the current one, unfortunately).
One of the most dangerous things I've seen are pieces of code that loop through object arrays and lists, same time expecting that array or list contains only correct elements. What do you think about ArrayList by example? Is it able to hold only those objects that are correct ones in current context or is ArrayList more powerful and can hold also incorrect objects?
Well, I'm always very suspicious when I see ArrayList or IList on .Net 2.0 code. I always ask myself one question: isn't it possible to refactor this code so there is List<ImportantClass> instead of ArrayList or IList? If it is possible th
Using Version Class 2008-04-02 18:41:00 When dealing with version numbers we often need to convert them to string and vice versa. There is lot of code where versions are handled manually in code. I don't know why. But I know for sure there is class called Version and I'm sure this class will help us a lot. Let's see a little example. The following code:
Version ver = new Version("1.4");Console.WriteLine("Major: " + ver.Major.ToString());Console.WriteLine("Minor: " + ver.Minor.ToString());Console.WriteLine("String: " + ver.ToString());Console.ReadLine();
outputs something like this:
Major: 1Minor: 4String: 1.4
As we can see, this class makes using versions very easy. There is one nasty limitation - Version class is sealed. If there is need for non-sealed class then you should write it by yourself.
SharePoint: Changing comments of document versions in code 2008-04-05 02:19:00 During one SharePoint migration project I had problem with documentversions
comments. I needed some way to set these in my code. As I didn't found any normal way to do it through SharePoint API I worked out something I call dirty hack. But it works.
The Idea
As API seemed more and more hopeless in my context I checked out what's going on in database and I discovered that I can set those version comments also after importing all the data. My hack affected only two tables in SharePoint database and after solving some mysteries I got everything work as expected.
Database
The first thing we will look at is SharePoint database. We are interested in two tables: AllDocs and AllDocVersions. Version fields are UIVersion in first table and Version in second table. Both tables have field called Chec Read more:Changing
SharePoint import ruins create and modification dates 2008-04-10 07:33:00 When exporting and importing data between SharePoint Server 2007 webs I found interesting bug - item creation and last modification dates
will be lost during import. Example - document is create
d at 05.04.2008. After import I checked out data in manifest.xml. Everything seemed to be correct - also modification and creation dates of documents. After export these dates are incorrect. Instead of original dates I can see date and time of export instead of original dates. Dirty guy as I am I also sniffed around in SharePoint database - I saw correct dates there! But when looking my document library through browser I saw incorrect dates. Does anybody know what is the problem? Is it bug and is there any workaround to this problem?
Unity and singletons 2008-04-13 14:17:00 I just putted up Unity
behind my test project and had some troubles with singletons. There is some misleading information in documentation you should be aware of. I created one class and one interface, both in same project to save some time.
namespace mytest { public interface ITest { string Name { get; set; } } public class Test : ITest { public string Name { get; set; } } }
So, as we can see, it's nothing special. Now let's take application configuration file. You can see tag called lifetim
Why MOSS2007 Content Import Fails? 2008-04-14 17:42:00 And another discovery about SharePoint 2007 content deployment. You should be aware of forbidden file types. When making mass import of content then import fails when it finds forbidden file type. All files after first forbidden file will not be imported. I don't know if this blog entry has some value or not but maybe I will save somebody's time about hours or days :) Read more:Content
, Import
SharePoint: Save performance when getting list items 2008-04-18 06:22:00 Another day, another solved mystery. Developers have interesting lifes as long as there are problems to solve. Another surprise from SharePoint - how to bog server down using only couple of lines of code? Of course, I have solution too that eliminates the issue. Let's say you have large document library with many documents (let's say there is a little bit more than 90.000 documents). Some of them are versioned, some of them are new and so on. Now, let's say you have to manipulate documents in your code and you have to find document with specific ID. The document, if it exists, is given to you as SPListItem: ... SPListItem item = list[new Guid(itemGuidString)]; ... When you try something like this the code may run fast - as long as you have few documents you don't have any per
SQL Server 2008 Reporting Services - save your time! 2008-04-19 16:16:00 Some time ago I tried out SQL
Server 2008 ReportingServices
and I was amazed. I'm serious MS Paint terrorist and nice pictures are not one of my strong skills. So, if something makes things look nice to me I'm very happy. I downloaded last CTP of SQL Server
2008 and installed it on my Virtual PC that runs Windows Server 2008. There were no problems during installation and after installing I was able to create my sample database. I also installed Reporting Services. During installation the reporting services web site was created on my server. One thing we got is new Report Designer. It looks like application form Office family, specially user interface. It is easy and user friendly. After setting up data source I was able to start building the reports. Now comes the most exciting pa
SharePoint: Half-Initialized Folders 2008-05-03 17:24:00 I discovered something weird when trying to detect document library folder by URL. It is possible to create folder objects that are half initialized. I tried to ask folder by URL and I used URL of documents list view. The result - folder with correct name etc but many parameters were missing. By example folder.Item returned me null. I got exception when I wanted to ask folder's unique ID. There were also many other parameters missing. But some parameters were correct. Somewhy I got half-initialized bastard instead of correct folder object I expected. Solution was simple - when asking folder by URL you have to give same URL you can use to open the folder in Windows Explorer. Using these URL-s you get always correct folder objects from web object. Read more:Folders
SharePoint: Assigning values to DateTime fields 2008-05-13 16:31:00 SharePoint list items have a tricky way how to assign values to DateTime fields in your code. It is possible to assign value of DateTime type to this field but it also possible to assign a string. This string is a little bit mysterious. You may get errors with one dates but some dates will work perfect. So what's the trick? If you want to assign date as a string to SharePoint's DateTime type fiel
ASP.NET Search Sitemaps - something for SEO 2008-05-12 18:29:00 ASP.NET Futures introduced support for search engine sitemaps. This is content detection protocol introduced originally by Google and later accepted also by Microsoft and Yahoo! ASP.NET makes sitemaps generating easy for us and as a bonus their model is perfect for more complex web applications like e-commerce and e-services sites. Let's see now what we can do. Couple of word about search sitemap Read more:Search
, Sitemaps
, something
, SEO
Extension method for enumerators 2008-05-12 17:09:00 Extensions method
s are pretty powerful thing because we can extend existing types without inheriting from them. I don't know developers who want to extend string or int or other basic types offered by .Net Framework. There was a project lately where I had to use existing code from internet. There was extremely chaotic and confusing error handling logic and try-catch blocks appeared even in place Read more:Extension