Tuesday 24 April 2012

Modal Popup in SharePoint 2010

Hello,
To create a modal popup in sharepoint 2010, follow these steps:

1. Create a visual webpart project.
2. In .ascx, wrtie the following code:
   <script type="text/javascript">
   function OpenDialog(URL) {
    var NewPopUp = SP.UI.$create_DialogOptions();
    NewPopUp.url = URL;
    NewPopUp.width = 900;
    NewPopUp.height = 500;
    SP.UI.ModalDialog.showModalDialog(NewPopUp);
  }
 </script>
 <asp:Button ID="btnOpenDialog" Text="Open Dialog"  
      OnClientClick="javascript:OpenDialog('/_layouts/settings.aspx'); return false;" runat="server" />

3. Deploy the webpart.

When you add the webpart on a page, and click on "Open Dialog" button, we will be noticing the page is opening in a modal popup window.



Monday 23 April 2012

SharePoint 2010 Master Pages - Retrieve, Upload and Apply programmatically


protected void Page_Load(object sender, EventArgs e)
{
 try
 { 
  if (!(Page.IsPostBack))
  {
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     using (SPSite pageLoadSite = new SPSite(SPContext.Current.Site.ID))
     {
      pageLoadSite.AllowUnsafeUpdates = true;

      using (SPWeb pageLoadWeb = pageLoadSite.OpenWeb())
      {
       pageLoadWeb.AllowUnsafeUpdates = true;

       GetMasterPages(pageLoadWeb);
       UploadMasterPageInMasterPageGallery(pageLoadWeb);
       SetMasterPage(pageLoadWeb);

       pageLoadWeb.AllowUnsafeUpdates = false;
      }
     pageLoadSite.AllowUnsafeUpdates = false;
    }
   });
  }
 }
 catch (Exception ex)
 {
  Throw ex;
 }
}


/// <summary>
/// Retrieves master pages from the master page gallery
/// </summary>
/// <param name="web">spweb object</param>
private void GetMasterPages(SPWeb web)
{
 try
 {
  SPDocumentLibrary masterPageGallery =        
      (SPDocumentLibrary)web.GetCatalog(SPListTemplateType.MasterPageCatalog);

  foreach (SPListItem masterPage in masterPageGallery.Items)
  {
    string displayName = masterPage.DisplayName
    string name = masterPage.Name;
  }

   
   string currentMasterFileName =  
          web.MasterUrl.Substring(web.MasterUrl.LastIndexOf('/') +    
                                  1);
 }
 catch (Exception ex)
 {
  throw new Exception("Exception in RetrieveMasterPages(). Message: " +  
                      ex.Message);
 }
}


/// <summary>
/// Uploads the master page file to master page gallery.
/// </summary>
/// <param name="web">spweb object</param>
private void UploadMasterPageInMasterPageGallery(SPWeb web)
{
 try
 {
  // fuMasterPage is upload control in my project.
  SPDocumentLibrary masterPageGallery =     
       (SPDocumentLibrary)web.GetCatalog(SPListTemplateType.MasterPageCatalog);
  masterPageGallery.RootFolder.Files.Add(
                    masterPageGallery.RootFolder.ServerRelativeUrl 
                            + "/"  
                            + fuMasterPage.FileName, 
                    fuMasterPage.FileBytes,                        
                    true);
  masterPageGallery.Update();
 }
 catch (Exception ex)
 {
    Throw ex;
 }
}


/// <summary>
/// Sets master page for the site.
/// </summary>
/// <param name="web">Website for which master page is to be set.</param>
private void SetMasterPage(SPWeb web)
{
 try
 {
  web.MasterUrl = string.Format("{0}/_catalogs/masterpage/{1}",  
                                 web.ServerRelativeUrl.TrimEnd('/'),          
                                 "MyMater.Master");
  web.Update();
 }
 catch (Exception ex)
 {
  throw new Exception("Unable to set master page for site "
                        + web.Title   
                        + ". Error: " +  
                     ex.Message);
 }
}

SharePoint 2010 THEMES - Retrieve, Upload and Apply programmatically



protected void Page_Load(object sender, EventArgs e)
{
 try
 {
  if (!(Page.IsPostBack))
  {
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
    using (SPSite pageLoadSite = new SPSite(SPContext.Current.Site.ID))
    {
     pageLoadSite.AllowUnsafeUpdates = true;

     using (SPWeb pageLoadWeb = pageLoadSite.OpenWeb())
     {
      pageLoadWeb.AllowUnsafeUpdates = true;

      GetThemes(pageLoadWeb);
      UploadThemeFileInThemeGallery(pageLoadWeb)
      SetTheme(pageLoadWeb);

      pageLoadWeb.AllowUnsafeUpdates = false;
     }
    pageLoadSite.AllowUnsafeUpdates = false;
    }
   });
  }
 }
 catch (Exception ex)
 {
  Throw ex;
 }
}


/// <summary>
/// Gets the list of themes from themes gallery.
/// </summary>
/// <param name="web"></param>
private void GetThemes(SPWeb web)
{
 try
 {
  SPWeb rootWeb = web;
  //Themes are available only at root-web level.
  if (!web.IsRootWeb)
    rootWeb = web.Site.RootWeb;

  SPDocumentLibrary themeGallery =  
        (SPDocumentLibrary)
                rootWeb.GetCatalog(SPListTemplateType.ThemeCatalog);

  if (themeGallery != null)
  {
   foreach (SPListItem themeItem in themeGallery.Items)
   {
     string themeName = themeItem.Name;
   }
  }
  else { // "No themes available for site " }

  //gets current applied theme Name
  string currentAppliedTheme = string.Empty;

  string themeurl = ThmxTheme.GetThemeUrlForWeb(web); //gets current 
                                                      // theme

  if (!string.IsNullOrEmpty(themeurl))
  {
   ThmxTheme theme = ThmxTheme.Open(web.Site, themeurl);
   currentAppliedTheme = theme.Name; //Gets the name of theme
  }
 }
 catch (Exception ex)
 {
  throw new Exception("Unable to get theme for site " + web.Title + ". Error: " + ex.Message);
 }
}


/// <summary>
/// Sets theme for the site.
/// </summary>
/// <param name="web">Website for which master page is to be set.</param>
private void SetTheme(SPWeb web)
{
 try
 {
  ThmxTheme theme = ThmxTheme.Open(web.Site, "_catalogs/theme/" + "MyTheme.thmx");
theme.ApplyTo(web, false);
web.Update();
 }
 catch (Exception ex)
 {
 throw new Exception("Unable to set theme for site " + web.Title + ". Error: " + ex.Message);
 }
}


/// <summary>
/// Uploads new theme file in theme gallery, overwrites, if already exists.
/// </summary>
/// <param name="web">Website for which master page is to be set.</param>
private void UploadThemeFileInThemeGallery(SPWeb web)
{
 try
 {
  //fuTheme is a fileupload control in my project
  SPDocumentLibrary themeGallery = (SPDocumentLibrary)web.GetCatalog(SPListTemplateType.ThemeCatalog);
  themeGallery.RootFolder.Files.Add(themeGallery.RootFolder.ServerRelativeUrl + "/" + fuTheme.FileName, fuTheme.FileBytes, true);
  themeGallery.Update();
 }
 catch (Exception ex)
 {
  throw new Exception("Unable to upload theme for site " + web.Title + ". Error: " + ex.Message);
 }
}