Pages

Aug 23, 2010

Changing a Site Collection URL...!

There is no out-of-the-box command in SharePoint to rename a site collection URL. So if you have a site created at http://team/sites/site1 and want to rename it to http://team/sites/site2, you will need to do a backup of the site collection, delete the site collection and restore the backup under a new site collection URL.

The command line for doing this would look like:

  1. stsadm –o backup –url http://team/sites/site1 -overwrite -filename backupsite1.dat
  2. stsadm –o deletesite –url http://team/sites/site1
  3. stsadm –o restore –url http://team/sites/site2 -filename backupsite1.dat

If you try to restore to another site on the same content database without first deleting the old one, you end up with an error message saying: [No content databases are available for this operation. Create a content database, and then try the operation again. To create a content database, click "Content databases" on the Application Management page, select the Web application to us e, and then click "Add a content database".] This is because you would end up with conflicting GUIDs on the same content database. That's why you need to delete the old site before the restore. You could also use a separate content database.

Also note that there is no web interface for this, you need to do this from a command line. Be sure to try this out on a sample test site before you actually move forward on a production environment.

Aug 3, 2010

SPUtility.SendEmail vs. SmtpClient.Send

When there is requirement to send email in SharePoint , developers usually use

Microsoft.SharePoint.Utilities.SPUtility.SendEmail(web, false, false, emailId, MailSubject, htmlBody);

due to the fact that this class automatically uses the default SMTP configuration settings of the SharePoint.

Developers avoid using System.Net.Mail.SmtpClient class to send emails because they need to have the SMTP configuration before using this class. However they can use the below code snippet to automatically detect the SMTP settings with the help of SPWebApplication.

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

message.IsBodyHtml = true;

message.Body = html;

message.From = new System.Net.Mail.MailAddress(SPContext.Current.Site.WebApplication.OutboundMailSenderAddress);

SPOutboundMailServiceInstance smtpServer = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance;

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer.Server.Address);

message.To.Add(emailId);

message.Subject = "Test";

smtp.Send(message);

A major drawback of using SPUtility.SendEmail is of its character limitation of 2048 per line which strips out the content of the Email after sending while System.Net.Mail.SmtpClient does not have any such limitation.

SharePoint Bug? SPListItem.Url.

If you request the Url property of an SPListItem you'll retrieve an unexisting Url. This property seems to work only for docitems. Weird!

How get SharePoint 2007 SpListItem DispForm Url

It's tricky to get the link to the DispForm.aspx of an SpListItem from the SharePoint 2007 object model.
In this sample you can find a piece of code to get the exact list item's url:

using (SPSite site = new SPSite("http://yoursite"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[0];
SPListItem item = list.Items[0];
string ItemDispFormUrl = String.Concat(item.Web.Url, "/",
item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, "?id=", item.ID.ToString());
}
}

Jul 29, 2010

Retrieving large number of Items from sharepoint list

If you have to reterive a large number of Items and also need a better performance then you should use one of the methods below :

1. Using SPQuery
2. Using PortalSiteMapProvider Class

Lets see the examples for both the methods :
Our Query - Query to get all the Items in a list where Category is "Sp2007"

SPQuery -


// Get SiteColl
SPSite curSite = new SPSite("http://myPortal");
//Get Web Application
SPWeb curWeb = curSite.OpenWeb();
// Create a SPQuery Object
SPQuery curQry = new SPQuery();
// Write the query
curQry.Query = "
SP2007
";
// Set the Row Limit
curQry.RowLimit = 100;
//Get the List
SPList curList = curWeb.Lists(new Guid("myListGUID"));
//Get the Items using Query
SPListItemCollection curItems = curList.GetItems(curQry);
// Enumerate the resulting items
foreach (SPListItem curItem in curItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}

PortalSiteMapProvider class -
The class includes a method calledGetCachedListItemsByQuery that retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call.
The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list, stores the results in cache and returns them from the method call.

// Get Current Web
SPWeb curWeb = SPControl.GetContextWeb(HttpContext.Current);
//Create the Query
SPQuery curQry = new SPQuery();
curQry.Query = "SP2007";
// Get Portal Map Provider
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb.ServerRelativeUrl), PortalWebSiteMapNode);
// Get the items
pItems = ps.GetCachedListItemsByQuery(pNode, "myListName_NotID", curQry, curWeb);
// Enumerate all resulting Items
foreach (PortalListItemSiteMapNode curItem in pItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}

Cross-list queries with SPSiteDataQuery

Scenario:
You need to query cross-list items across multiple Web sites
Solution:

'SPSiteDataQuery' is more efficent for such a situation.
'SPQuery' should be used to query a particular list

Sample Code:


  1. SPWeb webSite = SPContext.Current.Web;
  2. SPSiteDataQuery query = new SPSiteDataQuery();
  3. query.Lists = "";
  4. query.Query = "" +
  5. "Completed";
  6. System.Data.DataTable items = webSite.GetSiteData(query);
  7. foreach (System.Data.DataRow item in items)
  8. {
  9. Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "
    "
    );
  10. }