Monday, June 11, 2012

SharePoint Batch Insert/Update/Delete Performance

When you want to do bulk transaction insert, update, or delete.
If you have to update a larger number of items its highly recommended to not use the Update method on every item. Instead – use the batch update function ProcessBatchData provided by SPWeb.

StringBuilder query = new StringBuilder();
for (int itemIx=0;itemIx<100;itemIx++) {
  query.AppendFormat("" +
          "{1}" +
          "New" +
          "Save" +
          "{2}" +
       "", i, listGuid, someValue, "urn:schemas-microsoft-com:office:office#");
SPContext.Current.Web.ProcessBatchData("" +
    "{0}", query.ToString())

For more details of using ProcessBatchData check url:

Adding XsltListViewWebPart to sitedefinitions

If you want to add XsltListViewWebPart with custom xslt to site definition:
  1. Create a web part and do all configuration in existing site. Then export the web part this web part need some modifications to add to site definition.
  2. Exported Web Part contains ListGuid Which is difference from site to other. Remove ListGuid. Add one from these properties  ListName, ListUrl, or ListDisplayName
    One of the List* properties (ListName, ListUrl, or ListDisplayName) must be present and must specify a reference to a valid list. If more than one property is present, the order of evaluation is ListName, then ListUrl, and then ListDisplayName.

Add SPNavigation Heading programmatically

If you want to add header without link to SharePoint Navigation pro-grammatically,  Need to set some values in the NavigationNode.Properties HashTable.

            SPNavigationNode node, node1;
            // Get the parent’s top link bar.
            SPNavigationNodeCollection topnav = web.Navigation.TopNavigationBar;

            // Create the node.
            node = new SPNavigationNode("News", "/news", true);

            node = new SPNavigationNode("Services", string.Empty, true);
            node = topnav.AddAsLast(node);
            node1 = new SPNavigationNode("IT Support", "/Services/Lists/Helpdesk Initiation/NewForm.aspx?RootFolder=&source=/", true);

public static class SPNavigationNodeExtensions
    public static void MakeHeaderNode(this SPNavigationNode node)
        node.Properties["BlankUrl"] = "True";
        node.Properties["LastModifiedDate"] = DateTime.Now;
        node.Properties["Target"] = "";
        node.Properties["vti_navsequencechild"] = "true";
        node.Properties["UrlQueryString"] = "";
        node.Properties["CreatedDate"] = DateTime.Now;
        node.Properties["Description"] = "";
        node.Properties["UrlFragment"] = "";
        node.Properties["NodeType"] = "Heading";
        node.Properties["Audience"] = "";
As you can see, the “BlankUrl'” = True, and NodeType = “Heading”.

Sunday, June 10, 2012

Update PublishingImages using list.asmx

It takes from me too long time before success update PublishingImage Column using list.asmx.
We should pass value relative url with alt separated by comma. This is also applied for url, lookup, or user.
Check example below:
 string xml = ""+
        "    1"+
        ", test"+

"       ";

Friday, June 1, 2012

SharePoint designer 2010 soap:Server was unable to process request. ---> Value does not fall within the expected range

When try to edit any page using SP Designer error  soap:Server was unable to process request. ---> Value does not fall within the expected range appear. 

When you face the following error check:
  1. Check from Site Collection Administration SharePoint Designer Settings and Enable the following:
    • Enable Detaching Pages from the Site Definition
    • Enable Customizing Master Pages and Page Layouts
    • Enable Managing of the Web Site URL Structure
  2.  Check Url exist in alternate access mapping.
  3. Check your dns client.

XSLT template display part of article as summary

When created news, articles, and etc..., I want to take part of article as summary displayed in listing and headlines.I Created XSLT which can be used for this functionality:

<xsl:template name="trimPara">
    <!-- pass the string to be cropped with required string length -->
    <xsl:param name="stringLenRequired"/>
    <xsl:param name="stringInput"/>
    <xsl:value-of disable-output-escaping="yes" select="substring($stringInput,1,number($stringLenRequired))"/>
    <xsl:value-of disable-output-escaping="yes" select="substring-before(concat(substring($stringInput,number($stringLenRequired)+1,number($stringLenRequired) +20),' ' ),' ')"/>
    <!-- to make it a full word -->
    <xsl:if test="string-length($stringInput) > number($stringLenRequired)">...</xsl:if>

You can call this template by the following:

   <xsl:call-template name="trimPara">
            <xsl:with-param  name="stringLenRequired">200</xsl:with-param>
            <xsl:with-param name="stringInput" select="@Body"/>

Access WebParts using HttpContext from Console Application

I Created Console Application to export WebParts from SharePoint but I faced problem with web parts used HttpContext like CQWP (With Console Application HttpContext.Current is null). I cannot read web part properties. After some investigation I found the solution create a dummy HttpContext with the following code:

  if (HttpContext.Current == null) {
      HttpRequest request = new HttpRequest("", web.Url, "");
      HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
      HttpContext.Current.Items["HttpHandlerSPWeb"] = web;

Don't forget to set HttpContext.Current to null after finishing.
Now I can access web parts from console application.

Cannot Convert Type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor – Beware of this in custom master page for custom publishing site definition

When you create site definition or custom master page as feature you may face error like:

Cannot Convert Type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor – Beware of this in custom master page for custom publishing site definition.
When you export master page through SharePoint designer or WebDAV, It will add designer attributes (__designer) which will cause these problem.
<SharePoint:FieldValue FieldName="Title" runat="server" __designer:Preview="XXXXX" __designer:Values="XXXXX"/>

Also when open custom physical master page in SharePoint designer it adds __designer:Preview and __designer:Values attributes to my FieldValue and Literal controls, and if I don't remove them before check in ASP.NET complains.

 Also It may occur when you use html tags as below:

<a href="<%$SPUrl:~sitecollection/Pages/YourLink.aspx%>" >View All</a>

check url:

  1. Copy content instead of export from SPD.
  2. Double Check file and make sure you removed these attributes (__designer) 
  3. Look at the page directives in the top of the file, and ensure that a tilde and forward slash are present in the path(s):
    <%@ Register TagPrefix="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>

Use Content Query WebPart with site definition

When you want to create site using custom site definition and want to use Content Query WebPart, Export definition of your web part from existing site. You will find list is defined by Id called ListGuid. But the ListGuid is never the same when you create a new List. You need to do the the following steps before adding exported web part to your onet.xml:
  1. Remove ListGuid
    <Property name="ListGuid" type="string"></Property>
  2.  Add ListName and WebUrl (if they are not exist)
    <Property name=”ListName” type=”string”>Posts</Property>
    <Property name=”WebUrl” type=”string”>~site/</Property>
  1. ~site represent current site. you can use ~sitecollection to represent current site collection.
  2. ~site/ I added slash because if we are in root WebUrl will be blank and CQWP will not be rendered.