Thursday, May 30, 2013

SharePoint 2010 - Refresh the parent page using Server side API !

1.  Add the following code in elements.xml 

function openSitePropertyDialog(url) { 
       var dialogOptions = SP.UI.$create_DialogOptions(); 
       dialogOptions.width = 800; 
       dialogOptions.height = 400; 
       dialogOptions.dialogReturnValueCallback = Function.createDelegate(null, sitePropertiesDialogCallback); 
       commonModalDialogOpen( 
               url, 
               dialogOptions, 
               sitePropertiesDialogCallback, 
               null); 
   } 
   function sitePropertiesDialogCallback(dialogResult, returnValue) { 
       if (returnValue != null) { 
           var notificationId = SP.UI.Notify.addNotification(returnValue, false); 
       } 
       SP.UI.ModalDialog.RefreshPage(dialogResult); 
       //refreshUpdatePanelOnDialogCallback(dialogResult); 
   } 
   function refreshUpdatePanelOnDialogCallback(dialogResult) { 
       if (dialogResult == SP.UI.DialogResult.OK) { 
           __doPostBack(<%SPHttpUtility.AddQuote(SPHttpUtility.NoEncode(SitePropertiesUpdatePanel.ClientID),Response.Output);%>); 
       } 
   } 

2.  In code-behind do the following

if (!Page.IsValid) 
           { 
               return; 
           } 
 
           //Get propertyName and propertyValue to add 
           string propertyName = NameInputFormTextBox.Text.Trim(); 
           string propertyValue = ValueInputFormTextBox.Text.Trim(); 
 
 
           if (String.IsNullOrEmpty(propertyName)) 
           { 
               NameErrorLabel.Text = "Site property name cannot be empty."; 
               return; 
           } 
 
 
           try 
           { 
               if (Web.GetProperty(propertyName) != null) 
               { 
                   NameErrorLabel.Text = "Site property name already exists."; 
                   return; 
               } 
 
 
               Web.AddProperty(propertyName, propertyValue); 
               Web.Update(); 
           } 
           catch (Exception ex) 
           { 
               throw new SPException(ex.Message); 
           } 
 
 
           //Refresh the page when the dialog is closed by write JS directly to the 
           //http Response object that will close the dialog          
           if (SPContext.Current.IsPopUI) 
           { 
               Response.Clear(); 
               Response.Write(String.Format(@"<script language=""javascript"" type=""text/javascript""> 
 
               window.frameElement.commonModalDialogClose(1""{0}"");</script>", "Site property is successfully added.")); 
               Response.End(); 
           } 
           else 
           { 
               SPUtility.Redirect(strSource, SPRedirectFlags.UseSource, this.Context); 
           }           
Click here to download code from MSDN

No comments:

Post a Comment