Month: October 2009

Script to retract sharepoint solution package (wsp) and deactivate features

Home | SharePoint Administration | Script to retract sharepoint solution package (wsp) and deactivate features

The following snippet contains the script to retract sharepoint solution packages and de-activate features. This would be become handy to the sharepoint adminstrators, when they want to retract multiple solution packages (wsp) and de-activate multiple features.

:begin
@echo off

rem ** declare the solution to be retracted **
set solutionName=SampleSolution

rem ** declare the set of fetures to be de-activated **
set featureSampleFeature1=SampleFeature1
set featureSampleFeature2=SampleFeature2
set featureSampleFeature3=SampleFeature3

rem ** Replace this value with the URL of your site **
@set url=http://servername/sites/sitecollectioname/sitename

@set PATH=C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN;%PATH%

echo deactivating features in solution %solutionName%…
echo —————————————————-

stsadm -o deactivatefeature -name %featureSampleFeature1% -url %url% -force
stsadm -o deactivatefeature -name %featureSampleFeature2% -url %url% -force
stsadm -o deactivatefeature -name %featureSampleFeature3% -url %url% -force

echo Attempting to uninstallfeature and retract solution
echo —————————————————

echo Rectracting solution %solutionName% from solution store…

stsadm -o retractsolution   -name %solutionName%.wsp -immediate
stsadm -o execadmsvcjobs

echo Deleting solution %solutionName% from solution store…
stsadm -o deletesolution -name %solutionName%.wsp -override
echo.

if errorlevel == 0 goto :success
:success
echo Successfully deployed solution and activated feature(s)..
echo .
goto end

:end
pause

 

 Subscribe to my post

Create unsecure application pages for sharepoint

Home | SharePoint Development | Create unsecure application pages for sharepoint

There are times, where we need to create an application page to be accessed anonymously by the user, instead of the user signing-in and accessing the page. Here is the code-snippet to create an unsecure application page for sharepoint.

    public partial class UnsecureApplicationPage : Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase
    // inherit the page from Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase instead of Microsoft.SharePoint.WebControls.LayoutsPageBase    

    {

        protected void Page_Load(object sender, EventArgs e)
        {                   

        }       

    //override the allow anonymous property to true
        protected override bool AllowAnonymousAccess
        {
            get { return true; }
        }

        protected override bool AllowNullWeb { get { return true; } }

        protected override void OnPreInit(EventArgs e)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                base.OnPreInit(e);
                Microsoft.SharePoint.SPWeb _Web = SPControl.GetContextWeb(Context);
                this.MasterPageFile = _Web.MasterUrl;
            });

        }

    }

 Subscribe to my post

Create secure application page for sharepoint

Home | SharePoint Development | Create secure application page for sharepoint

Creating an application page for sharepoint is fairly simple and straight-forward. To make that application page secure, just do some extra steps in the code. Here is the code-snippet for creating secure application page in sharepoint .

public partial class SampleSecurePage : Microsoft.SharePoint.WebControls.LayoutsPageBase    

    {

        public SampleSecurePage()
        {
           //In the constructor define rights check mode to be done on the pre-init event  
            this.RightsCheckMode = RightsCheckModes.OnPreInit;

        }

        //get the permission set from spbase permission enumeration
        protected override SPBasePermissions RightsRequired
        {
        get
        {
        SPBasePermissions permissions = base.RightsRequired;
        return permissions;
        }
        }

        protected override void OnPreInit(EventArgs e)
        {

                 base.OnPreInit(e);
                 Microsoft.SharePoint.SPWeb _Web = SPControl.GetContextWeb(Context);

        //dynamically assign the master of the current spweb to the application page
                 this.MasterPageFile = _Web.MasterUrl;

             if (!HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    //re-direct the user to log-in page and authenticate

                }                  

        }       

        protected void Page_Load(object sender, EventArgs e)
        {
            //do some logic here             

        }        

    }

 Subscribe to my post

Leverage Javascript inside sharepoint web parts

Home | SharePoint Development | Leverage Javascript inside sharepoint web parts

Many a times, we’d find the need for leveraging the javascript in sharepoint web parts. It could be a simple requirement of opening up a pop-up window or could be  a complex requirement to invoke a web service from javascript. Here is code-snippet and steps to leverage java script inside sharepoint web parts. The crux is to embeed or register the javascript function in the PreRender event of the web part and invoke this registered javascript, from wherever desired.

//In the constructor of the web part, define a delegate of the PreRender method.
    public SampleWebPart()
        {
            this.PreRender += new EventHandler(SampleWebPart_ClientScript_PreRender);

        }

        #region register javascript methods

//In the delegate method of PreRender, call the another method that does the registering of javascript
        // Client script registration event
        private void SampleWebPart_ClientScript_PreRender(object sender, System.EventArgs e)
        {
            RegisterJavaScript();
        }
//Use the RegisterClientScriptBlock method of the Page object, to register the javascript
        //Function will embedded script
        protected void RegisterJavasScript()
        {

            //format the string to hold the javascript
            string embeedscript = "   <script>" +
                                    "function openNewWin(url) {" +
                                        //"var x = window.open(url, 'mynewwin', 'width=1200,height=800');" +
                                        "var x = window.open(url);" +
                                        "x.focus();" +
                                            "} " +
                                   "</script>";

            string OpenScriptKey = "OpenNewWin";
            //check whether the javascript has been already registered to the page
            if (!Page.IsClientScriptBlockRegistered(OpenScriptKey))
            {
                Page.RegisterClientScriptBlock(OpenScriptKey, embeedscript);

            }

        }

protected void Button1_click(object sender, ImageClickEventArgs e)

        {

//invoke the javascript function that opens the application page  in new window
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");                

}

 Subscribe to my post