Friday, May 30, 2014

Check Sharepoint page is in edit mode or not

Today I came across one of the scenario where i need to check page is in edit mode or not.
Scenario: We have sharepoint custom webpart which needs to be cached. Problem here was if the user update the any of the webpart property it was not reflecting automatically(another way we can achieve this was using webpart tool property)

Because, in the create child control method we are checking whether the control is cached or not, since the webpart was available in the cache this was not rebuilding the control.

So solution for this problem is if page is in edit mode then rebuild the webpart else check if the webpart is available in the cache if it is available then get it from there else rebuild the control.

//The below code will work if we are using pages which are associated to page layout
if(Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit)
{
         //Page is in edit mode
         BuildTheWebpart();
}
else
{
         //Page is in display mode 
         if(WebpartIsAvailableInCache())
         {
              //Get the webpart from cache and render it
         }
         else
         {
              BuildTheWebpart();
         }
}


What about if it is simple webpart page?

WebPartManager wpManager = this.WebPartManager;
if (wpManager.DisplayMode == WebPartManager.EditDisplayMode)
{
//This indicated page is in edit mode
BuildTheWebPart();
}
else
{
  //Page is in display mode
         if(WebpartIsAvailableInCache())
         {
              //Get the webpart from cache and render it
         }
         else
         {
              BuildTheWebpart();
         }
}

But none of the above code didn't solve my problem. The page was provisioned through feature and its not associated to any page layout it just provisioned in the page library.

The simplest fix i found out is check page is checked out or not


Usually for all sharepoint sites this will be activated by administrator so we can use this to check page is in edit mode or not 


if(Microsoft.SharePoint.SPContext.Current.FileLevel = SPFileLevel.CheckOut)
{
     //Edit mode
     Build the control
}
else
{
    //page is checked in so user is not updating any of the webpart property
}



Hopefully if any one face the issue like me can use this check to determine page is in edit or not.

Thursday, May 15, 2014

Connecting Social Media in Sharepoint

I have created Social webpart which will interact with Facebook and twitter. This has been displayed in tabs, when user click on Facebook its related post will display if the user clicks on Twitter then all the tweets will display. I have uploaded the source code/project in codeplex, please provide the feed back/comments.

https://sharepointsocialcontrol.codeplex.com/

Tuesday, May 13, 2014

How to register Javascript & CSS file in custom webpart

In this post will explain about how we can register CSS and Javascript files in the webpart code.

What are the advantages of registering CSS and JS file through webpart?

Lets assume if we register CSS and JS file in masterpage every time this files will be loaded irrespective of whether webpart rendered in the page or not. This will be increase response time for the page. To overcome this we can register CSS and JS file in the webpart.

How we can achieve this?

If we are using custom webpart then in the on pre render method we can use below snippet of code to register the CSS & JS file.


protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            try
            {
                //This is the sharepoint layout mapped folder which contain jQuery and javascript file
                ScriptLink.Register(this.Page, "/_layouts/Scripts/jquery-1.9.1.min.js", false);
                ScriptLink.Register(this.Page, "/_layouts/Scripts/customJS.js", false);

                CssRegistration wpCss = new CssRegistration();
                //Location of the CSS file path
                wpCss.Name = "FilePath";
                //Lets assume we need to load the webpart CSS file after the main CSS file or it may                   be after core.css file
                wpCss.After = "MainFileName";
                this.Controls.Add(wpCss);
            }
            catch (Exception ex)
            {
            }
        }