Thursday, May 30, 2013

Sharepoint pop up window from code behind in visual webparts

You can use the below piece of code to open a sharepoint pop up window from code behind .This is done by calling a jaavascript function on button click from the code behind

In the ascx page use the below code
<script type="text/javascript">
function OpenDialog(strPageURL, width, height, title, args) {
ExecuteOrDelayUntilScriptLoaded(test, "sp.js")
function test() {
var dialogOptions = SP.UI.$create_DialogOptions();
dialogOptions.url = strPageURL; // URL of the Page
dialogOptions.width = width; // Width of the Dialog
dialogOptions.height = height; // Height of the Dialog
dialogOptions.title = title;
dialogOptions.args = args;
dialogOptions.allowMaximize =
false;
dialogOptions.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
// Function to capture dialog closed event
SP.UI.ModalDialog.showModalDialog(dialogOptions); // Open the Dialog
return false;}
}


// Dialog close event capture function
function CloseCallback(strReturnValue, target) {

if (strReturnValue === SP.UI.DialogResult.OK) // Perform action on Ok.
{//alert("User clicked Ok!");
}if (strReturnValue === SP.UI.DialogResult.cancel) // Perform action on Cancel.
{//alert("User clicked Cancel!");}
}
 
</script>

In the button click event call the below code  

var url = "http;//www.google.com";

ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "OpenDialog('" + url + "', 994, 1000, 'Title', null);", true); 


We are using the code
ExecuteOrDelayUntilScriptLoaded(test, "sp.js")

because the sharepoint API for sharepooint modal pop up dialog window is present in  sp.js sometime this is loaded after the javscript function is called which will result in error.Hence inorder to get over it we force the loading of sp.js before the function is executed ...


Also add below links in page

<SharePoint:ScriptLink ID="ScriptLink1" Name="sp.ui.dialog.js" LoadAfterUI="true" Localizable="false" runat="server"></SharePoint:ScriptLink>
<%--<Sharepoint:ScriptLink ID="ScriptLink2" Name="SP.UI.Dialog.debug.js" LoadAfterUI="true" Localizable="false" runat="server"></Sharepoint:ScriptLink>--%>
<SharePoint:ScriptLink ID="ScriptLink3" Name="sp.js" LoadAfterUI="true" Localizable="false" runat="server"></SharePoint:ScriptLink>




To redirect to page or close popup on button click see the link Close popup

Error in content type deployment while visual studio deployment .Content type Id already exists for this feature ID

I encountered a starnge error.I just created a content type through Visual studio 2012 using some site columns deployed it ,it worked fine.But now I made some changes and try to redeploy it.It poped up with a errror stating "Content type with the ID ______ in the feature ID ________ already exists"


I tried retract and deploy again and again it dint work

The trick was .

1) Retract the solution
2)Close the Visual studio
3)Restart and redploy
4)It will work fine .

Wednesday, April 24, 2013

Customise List view webpart using custom XSL file in Sharepoint - 2013

I had this requirement of customising the XSLT list view webpart .Where in I had to redirect the users with few query string to a page.
Usually I used to go ahead with adding the List view webpart into the page by using the browser and edit the same page using the sharepoint designer later I would start editing the XSLT of the webpart using the design-->Edit XSLT view option in the designer .That does the job

But this time I tried to customise the XSLT by adding the cutom XSL file as link in the WebPart property of the List view webpart in the browser the Property name is "XSL Link".

Here are the steps how I achieved this
1)Create a blog site
2) Added a categories webpart as I wanted to customise this webpart .
3)Opened Visual studio and create new XSL file
4)Added the below code in the XSL file and saved it as testCustomise.xsl




<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
 <xsl:output method="html" indent="no"/>

 <xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">


  <xsl:variable name="TempSortedRows">
        <xsl:for-each select="/dsQueryResponse/Rows/Row">
          <xsl:sort select="@Title" order="ascending"/>       
    <xsl:copy-of select="."/>
   </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="SortedRows" select="msxsl:node-set($TempSortedRows)/*"/>
  <xsl:variable name="TotalRows" select="$SortedRows"/> 
    <xsl:call-template name="DisplayCategories">
   <xsl:with-param name="Rows" select="$SortedRows"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="DisplayCategories" ddwrt:ghost="" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
  <xsl:param name="Rows" />
        <ul>
   <xsl:for-each select="$Rows">
    <xsl:variable name="thisNode" select="."/>
    <xsl:variable name="catid" select="$thisNode/@ID"/>
    <xsl:variable name="catName" select="$thisNode/@Title"/>
    <xsl:variable name="targeturl" select="concat('Lists/Categories/Category.aspx?CategoryId=',$catid, '&amp;','Name=', $catName)" />
    <li><a href="{$targeturl}"> <xsl:value-of select="$thisNode/@Title" /></a></li> 
   </xsl:for-each>

  </ul>
 </xsl:template>
</xsl:stylesheet>


U can change the design using any HTML tags in "DisplayCategories" template name is the tag which describes how your design looks like in the webpage.You can go use any Coulmns I have made customisation to display only the Title  columns as anchor tag which will be redirected to categories page with few query strings .You can achieve any design or funcatinallity just by editing this XSL.


Later Since I was working in SharePoint-13 I placed the testCustomise.xsl in 15/Layouts/XSL .

Now You go back to browser

5)Edit Page-->Edit category List view webpart --> in the webpart property -->XSL Link
6)Add this /_Layouts/15/xsl/testCustomise.xsl
7)then say apply and ok

Now you should be able to see those styling changes with the hyperlink to different page


If you are workin in sharepoint 2010 place the file in 14 hive xsl your link will look as
/_Layouts/xsl/testCustomise.xsl


You could also place the file in site assets or site library and provide the XSL file link of the same to the webparts .That will work too !!



Wednesday, April 3, 2013

Localization [Multilingual fuctionality] of Infopath forms-13 for SharePoint 13 using XML a resource file

To creata a multilingual Infopath forms which is to be used in the Sharepoint site here are the below steps.

Software used are Infopath Forms 13 and Sharepoint 13

Open the Infopath form Designer select  a Blank form template.Publish it to the required sharepoint 13 site as content type or to form library .

Create an XML file called "Resource.xml" from which we pic the language specific text to to displayed in the Sharepoint site .

Your xml file can look like below :::


<?xml version="1.0" encoding="UTF-8"?>
<
LangSettings><
Languages><
Language Name="English" Code="ENG"/><
Language Name="Spanish" Code="SPA"/><
Language Name="French" Code="FRE"/></
Languages><
Labels><
Label Name="Name"><
LocalLabel Code="ENG" Value="First Name"/><
LocalLabel Code="SPA" Value="Nombre"/><
LocalLabel Code="FRE" Value="Nom"/></
Label> <
Label Name="Phone Number"><
LocalLabel Code="ENG" Value="Phone Number"/><
LocalLabel Code="SPA" Value="Ph no in Spanish "/><
LocalLabel Code="FRE" Value="Ph no in German"/></
Label> </
Labels><
Colors><
LocalColor ID="Blue" Code="ENG" Value="Blue"/><
LocalColor ID="Blue" Code="SPA" Value="Azul"/><
LocalColor ID="Blue" Code="FRE" Value="Bleu"/><
LocalColor ID="Green" Code="ENG" Value="Green"/><
LocalColor ID="Green" Code="SPA" Value="Verde"/><
LocalColor ID="Green" Code="FRE" Value="Vert"/><
LocalColor ID="Red" Code="ENG" Value="Red"/><
LocalColor ID="Red" Code="SPA" Value="Rojo"/><
LocalColor ID="Red" Code="FRE" Value="Rouge"/><
LocalColor ID="Yellow" Code="ENG" Value="Yellow"/><
LocalColor ID="Yellow" Code="SPA" Value="Amarillo"/><
LocalColor ID="Yellow" Code="FRE" Value="Jaune"/></
Colors></
LangSettings>

To add the XML file into the Infopath open the Infopath form-->DATA-->From Other Sources-->From XML files.

Browse the XML file and add it .Click finish for finish close the window for everything .

Below are the different scenarios in Infopath forms where the multilingual func has to be taken care of

First add a drop down list to select the required language of the forms.Use the resource as data source and select code as value field and Name as display field .

  1. LABEL
We are not going to use the  label here.Instead add a "Calculated field"

it opens in a pop up Pick the "Name " field to be displayed in such a way that add conditions like 
Display "Name " from resource file where "label=Name" and where "Code=Selected language"
the equation will look like below

@Value[@Name = "First Name" and @Code = LanguageChoice] 

2) For Drop Down display

use the resource file "color" field
Bind the drop down with the "color" tag of resource file and filter in such a way as to get only the data for partilar language .This is done by coparing with the selected language to ID field


3)For Button

There is no direct way with the button

You got to add 3 different buttons  for three different language having same id  but different display name
on clicking the each button add a rule to format the control
In the conditon
  •  Hide hide control
  • Condtion will be like "slected language  if not equal to ENG" same for other language too expect the last condition will be SPA or FRE