31 Agu 2010

Simple LINQ Example on Sharepoint Items Query

Actually I don’t remember much about CAML that came up with Sharepoint. CAML is a basic XML based query to fetch some item based on criteria, just like query on SQL. But, It’s so hard to remember! I want to run code as fast as I can, and here’s the answer, LINQ. So, can we start and try this?

OK, before we start, we must ensure that we have .NET Framework 3.5, coz that’s the framework to do the LINQ, and import System.Core.dll to your project. and add the imports below.

using System.Collections;
using System.Collections.Generic;
using System.Linq;

LINQ gives you so many solution to get queries easily, and you don’t need to remember CAML or whatever you’ve called. You can get raw IEnumerable<SPListItem> from the LINQ, but if you want to iterate through query result, you must use foreach.

IEnumerable<SPListItem> LiColl = from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 &&
Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li;

Another way, you can get items, using IList<SPListItem> and you can iterate with any iterator.

IList<SPListItem> LiColl2 = (from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 && Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li).ToList();

And the other way, you can get items, and returning array of SPListItem.

SPListItem[] LiColl3 = (from SPListItem Li in Lhr.Items where Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days <= 0 && Convert.ToDateTime(Li[SPD.ReplaceFieldSpaceName("Tanggal Kelahiran")].ToString()).Subtract(DateTime.Today).Days >= (_DayIndex * -1) select Li).ToArray();

Rename Sharepoint Site URL using STSADM

Sometimes you faced a little problem with Sharepoint, and what if the problem is you do not like the existing Site Collection URL? For example, http://intern.contoso.com/sites/HRArea and you want to change it to http://intern.contoso.com/sites/HR ? Try this STSADM Commands…

The method is involving backup, delete and restore command in STSADM, so be careful to do this, and trying not to make any mistakes. First time, you must confirm the old URL and the new URL. Maybe you would like to try my another solution using STSADM, so you don’t need to write the full path of STSADM, coz it so annoying.

STSADM –o backup –url <the old url> –overwrite –filename <the backup filename>

So, the example would be like this, STSADM –o backup –url http://intern.contoso.com/sites/HRArea –overwrite –filename D:\BackupHR.dat. This will make backup for the old URL, including contents.

STSADM –o deletesite –url <the old url>

Make sure that the first command completed successfully, so you can do this command, to delete the old site URL. The example is STSADM –o deletesite –url http://intern.contoso.com/sites/HRArea.

STSADM –o restore –url <the new url> –filename <the backup filename>

And the next command is to restore the backed up site. So the site will run with the new URL. And you’re done….