Archive for July, 2010

Open SharePoint 2010 Dialog Box from Content By Query Web Part

July 25, 2010

 

Content By Query web part uses xml style sheets for UI and therefore allow us to customized its look and feel by modifying these style sheets. The style sheet which is applied to the items displayed in Content Query Web Part is “Item Style” and you can find it in the folder called “XSL Style Sheets” in the out of the box style library in your site collection.

image

Here is how you can add your own style to ItemStyle.xsl:

1. Download ItemStyle.xsl and open it using Visual Studio.

2. You’ll see multiple "<xsl:template” nodes each of which indicates a Item style.

3. You can create your own style by either copying & pasting existing style and customizing it or create your own style by scratch.

4. I have created my style by copying and customizing style called “Bullets” and following is how my customized style looks like:

<xsl:template name=MyBullets match=Row[@Style=’MyBullets’] mode=itemstyle>
        <
xsl:variable name=SafeLinkUrl>
            <
xsl:call-template name=OuterTemplate.GetSafeLink>
                <
xsl:with-param name=UrlColumnName select=‘LinkUrl’/>
            </
xsl:call-template>
        </
xsl:variable>
        <
xsl:variable name=DisplayTitle>
            <
xsl:call-template name=OuterTemplate.GetTitle>
                <
xsl:with-param name=Title select=@Title/>
                <
xsl:with-param name=UrlColumnName select=‘LinkUrl’/>
            </
xsl:call-template>
        </
xsl:variable>
        <
div class=item link-item bullet>
            <
xsl:call-template name=OuterTemplate.CallPresenceStatusIconTemplate/>
            <
a href=# title={@LinkToolTip}>
                    <
xsl:attribute name=onclick>
                        javascript:

                        function DialogCallback(dialogResult, returnValue)
                        {
                            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
                        }

                        function onQuerySucceded()
                        {
                        
                        var currentSiteUrl = this.site.get_url() + ‘/_layouts/MyPage.aspx?Source=<xsl:value-of select=@LinkUrl/>&amp;ItemId=<xsl:value-of select=@ID/>‘;

                        var options =
                        {
                        url: currentSiteUrl,
                        title: ‘My Dialog’,
                        allowMaximize: false,
                        showClose: true,
                        width: 600,
                        height: 300,
                        dialogReturnValueCallback: DialogCallback
                        };

                        SP.UI.ModalDialog.showModalDialog(options);
                        }

                        function onQueryFailed()
                        {
                        alert(‘Failed’);
                        }

                        var ctx = SP.ClientContext.get_current();
                        this.site = ctx.get_site();

                        ctx.load(this.site);

                        ctx.executeQueryAsync
                        (
                        Function.createDelegate(this, onQuerySucceded),
                        Function.createDelegate(this, onQueryFailed)
                        );

                    </xsl:attribute>
                <
xsl:value-of select=$DisplayTitle/>
            </
a>
        </
div>
    </
xsl:template>

I modified the hyperlink action and called the JavaScript to open a SharePoint dialog box. You can find lots of posts on how to open a SharePoint 2010 Dialog box and hence i am not including explanation for that here. Also, i have placed the JavaScript directly in xsl file but you can create a separate JS file and can call the JS method in the xsl file.

Notice that i have passed two query string parameters to my dialog page and used @LinkUrl and @ID for Item URL and Item ID respectively. You can do the experiments with your custom columns as well.

5. Upload the ItemStyle.xsl back in Style Library.

6. Now edit the Content By Query Web part and expand the "Presentation" group under content query tool part.

7. Here you will find the newly added style as “My Bullets”.

8. Select the style and you click ok. Now when you’ll click on the link of item in the web part it will open the your dialog page and will pass Item URL and Item Id as query string parameters to it.

Advertisements

Execute SharePoint Power shell commands from C# code

July 25, 2010

A friend of mine asked me yesterday how can we run a SharePoint power shell command like backup a site collection from a C# windows application and i decided to give it a try and here are the steps on how can we do it.

1. Add a reference to “System.Management.Automation” assembly in your project. I could not locate the assembly and a bit goggling suggested that add the following xml to your csproj file by editing it directly.

<Reference Include="System.Management.Automation" />

2. Add a using statement for following namespace:

using System.Management.Automation

3. Call following method on your button click event. This method executes “backup-spsite” command to backup a site collection. Modify the method by changing your site collection URL and backup file path and you are good to go.

  private void ExecutePowerShell()
        {
            PowerShell OPowerShell = null;
            Runspace OSPRunSpace = null;

            try
            {
                RunspaceConfiguration OSPRSConfiguration = RunspaceConfiguration.Create();
                PSSnapInException OExSnapIn = null;

                //Add a snap in for SharePoint. This will include all the power shell commands for SharePoint
                PSSnapInInfo OSnapInInfo = OSPRSConfiguration.AddPSSnapIn(“Microsoft.SharePoint.PowerShell”, out OExSnapIn);

                OSPRunSpace = RunspaceFactory.CreateRunspace(OSPRSConfiguration);

                OPowerShell = PowerShell.Create();

                OPowerShell.Runspace = OSPRunSpace;

                Command Cmd1 = new Command(“backup-spsite”);
                Cmd1.Parameters.Add(“identity”, “Your Site Coll URL”);
                Cmd1.Parameters.Add(“path”, “Back up file path”);

                OPowerShell.Commands.AddCommand(Cmd1);

                OSPRunSpace.Open();

                OPowerShell.Invoke();

                OSPRunSpace.Close();
            }
            catch (Exception Ex)
            {
                //Handle exception
            }
            finally
            {
                if (OSPRunSpace != null)
                {
                    OSPRunSpace.Dispose();
                    OSPRunSpace = null;
                }

                if (OPowerShell != null)
                {
                    OPowerShell.Dispose();
                    OPowerShell = null;
                }
            }
        }

Power shell is a wide subject and there are many things which you can do like executing the commands on remote machine, writing your own commands etc. I’ll write down about these in my future posts.