Pages

Search This Blog

Showing posts with label UI. Show all posts
Showing posts with label UI. Show all posts

Tuesday, February 21, 2012

Programatically set default Page layout of site to Custom page layout

In my previous post, we have learned how to create custom page layouts using feature.

Requirement:
We need to set the custom page layout created as the default page layout of the site.
Say if a user creates a new page, it should create a page which is using the custom layout.
We need to achieve this programatically.

Solution:

We can achieve this by writing the code within the receiver of the feature created.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{   
    SPSite site = properties.Feature.Parent as SPSite;
    SPWeb web = null;
    if (site == null)
    {
         web = properties.Feature.Parent as SPWeb;
         if (web == null) return;
         site = web.Site;
    }
    else web = site.RootWeb;

       //Get the publishing web object
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout  (InnerPage.aspx)             
    if (objPublishingWeb != null)
    {
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == "InnerPage.aspx"
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);
       objPublishingWeb.Update();
    }
}


Create Pages using Custom Page Layout & Deploy them in Pages library

In my last post I described how to create custom page layouts.
Now using those page layouts, we need to create Pages and deploy them in pages library of the publishing site.
Follow the below mentioned steps in order to achieve the above.
  • Create a new module within the solution (say MyPages)
  • Delete the sample.txt file.
  • Add a text file within the module and rename it to Home.aspx

  • Add the required directives in Home.aspx page and add web part zones within the page. You can download Home.aspx page here.
  • Open the Elements.xml file and make changes accordingly as shown below
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MyPages" Url="Pages">
    <File Path="MyPages\Home.aspx" Url="Home.aspx" Type="GhostableInLibrary" NavBarHome="True">
      <Property Name="Title" Value="Home" />
      <Property Name="PublishingPageImage" Value="&lt;img alt=&quot;Welcome&quot; src=&quot;~SiteCollection/PublishingImages/HomeBanner.jpg&quot; &gt;"></Property>
      <Property Name="PublishingPageLayout"
                   Value="~SiteCollection/_catalogs/masterpage/HomePage.aspx, Home Page" />
      <Property Name="ContentType" Value="MyCustomContentType" />    
    </File>
</Module>
</Elements>
  •  Deploy the solution and you should be able to see Home.aspx page created in the pages library using the custom page layout created.
  • We have specified that this page should be deployed to Pages library in our Elements.xml file of this module. (URL="Pages")

Tuesday, January 31, 2012

Color Coding for SharePoint Tasks Lists

In this post I will tell about how to give the color code to the tasks list which can be very attractive in knowing the status of the tasks.


  • Create a task list , in this example I have taken the default task list. 

  • Now create a column by the name "Color" and select the column type as "Calculated" and enter the following logic in the formula column and click Ok.
         =IF(OR(Status="Not Started",Status="Deferred"),"Yellow",IF(OR(Status="In  
        Progress",Status="Complete"),"Green",IF(Status="Waiting on Someone Else","Red")))



  • Now in the similar fashion create a new column by the name "Color Status", choosing column type as the "Calculated" and entering the logic in the formula section as given below.
          <DIV style='font-weight:bold; font-size:24px; color:Yellow;'>•</DIV>
  • Now your list will look like something like this:
  • Now create a text file and add the following code in that:
                <script type="text/javascript">
//
// Text to HTML
//
var theTDs = document.getElementsByTagName("TD");
var i=0;
var TDContent = " ";
while (i < theTDs.length) {
try {
TDContent = theTDs[i].innerText || theTDs[i].textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTDs[i].innerHTML = TDContent;
}
}
catch(err){}
i=i+1;
}
//
// ExpGroupRenderData overwrites the default SharePoint function
// This part is needed for collapsed groupings
//
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
var tbody=document.getElementById("tbod"+groupName+"_");
var wrapDiv=document.createElement("DIV");
wrapDiv.innerHTML="<TABLE><TBODY id=\"tbod"+ groupName+"_\" isLoaded=\""+isLoaded+ "\">"+htmlToRender+"</TBODY></TABLE>";
var theTBODYTDs = wrapDiv.getElementsByTagName("TD"); var j=0; var TDContent = " ";
while (j < theTBODYTDs.length) {
try {
TDContent = theTBODYTDs[j].innerText || theTBODYTDs[j].textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTBODYTDs[j].innerHTML = TDContent;
}
}
catch(err){}
j=j+1;
}
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);
}
</script>
  • Upload the text file on the sharepoint site (say in the shared document library)
  • Now add the task list webpart on the page you want to display.
  • Edit the webpart and select the columns that are of use.
  • Now add the content editor webpart right below the task list webpart (otherwise it will not work).
  • Edit the webpart and change the content link to the link where the text file was uploaded.(as i have uploaded in the shared document library)
  • Set the content editor webpart to be hidden and  chrome type to be none.
  • Now save the page and the list will look something like this.

Happy Coding..!!!

Monday, January 16, 2012

How to change the default home page or Set Welcome Page of a SharePoint site using PowerShell Script - SharePoint 2010

By default a SharePoint 2010 publishing site uses default.aspx as its welcome page.
There are so many conditions where we use our custom web part page as the default page for our publishing site in place of general default.aspx. The best example for this is an application dashboard as the default page.

Below are four ways to set another page as your home page: (all four work for both 2007 and 2010)

1. From Site Settings (If the publishing features are enabled)
2. From SharePoint Designer
3. From code / API
4. From PowerShell

1. If the publishing features are enabled for a site then:
Go to Site Actions -> Site Settings -> Welcome Page
In SharePoint 2007

In SharePoint 2010

2. From SharePoint Designer:
Right-click the new page and click "Set as Home Page".
(For SharePoint 2007 this only appears to work from SharePoint Designer if the file is in the root of the site. I.e. the same place as default.aspx.)

3. From code / API:

using (SPSite oSiteCollection = new SPSite("http://sharepoint2010:2400"))
            {
                SPWeb oWebsite = oSiteCollection.OpenWeb();
                SPFolder oFolder = oWebsite.RootFolder;
                oFolder.WelcomePage = "SiteAssets/BecomeFan.aspx";
                oFolder.Update();
                oWebsite.Dispose();
            }

4. From PowerShell:
In SharePoint 2010, Create a powershell script file e.g. SetWelcomePage.ps1, with the following script:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$assignment=Start-SPAssignment
$web=Get-SPWeb -Identity "http://sharepoint2010:2400" -AssignmentCollection $assignment
$rootFolder=$web.RootFolder
$rootFolder.WelcomePage="SiteAssets/BecomeFan.aspx"
$rootFolder.Update()
Stop-SPAssignment $assignment
$web.Update()
$web.Dispose()
Write-Host 'Welcome Page Set Successfully…'
Write-Host 'Press any key to exit…'
$x=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$Host.SetShouldExit(1)

After creating the script file, just run that file by right click and Run with PowerShell:


Now, navigate back to a site and see that the homepage is now set to the given page.


Monday, December 19, 2011

Create Custom Dictionary or Add custom Word in Dictionary for Site Collection SharePoint


Some times we have a requirement to add custom word in the SharePoint dictionary. Below are the steps to add your custom word in SharePoint Dictionary 




1.  Create a text file on your local machine and add each custom term on a seperate line, save the file as "Custom Dictionary.txt"



2.  Create a library within the root of the site collection, call it "Spelling"; upload your "Custom Dictionary.txt" file to the library



. 





Hope it helps!

Tuesday, June 14, 2011

Custom List forms - Change Newform.aspx or Editform.aspx with Custom Webpart

In this article I will show you how to use custom webpart as a replacement for NewForm.aspx or EditForm.aspx.
Sometimes we have a scenario like we have a dropdown box and the data-items coming for this dropdown has to come from a SQL Server database or some other database.
Or we have some business validations to put on the controls present in NewForm.aspx page or Editform.aspx page i.e. in List Forms.
So to tackle these kind of situations we need to customize our List forms.
We can customize the List-forms in two ways.
· Customize the List form through XSLT
· Change the Listforms with custom webpart which is having all your business requirements and a mechanism to add or edit items in List. This custom webpart we can build in Visual Studio through Sharepoint Object Model.
In this article I will tell you how to change the newform.aspx or editform.aspx with custom webpart.
Here I believe that you all know how to build custom webpart for Sharepoint 2007.
When we are click New button to add a new item




We are getting NewForm.aspx page. Now to change the list forms with some other webpart we need to edit the page in the same way which we are doing for a normal web part page but the problem here is we do not have Edit Page option under Site Action tab. Now what to do? How to open edit page mode?
The answer is very simple and little bit tricky also.


The URL for newform.aspx page will be like this:
just add ?&Toolpaneview=2 above URL. Now your URL becomes like this.
Now click on Go button in Internet Explorer and you will see the NewForm.aspx page like this:




Just close this webpart and add your new custom webpart here.
So after that whenever you will click on new button for adding a new item you will see your custom webpart instead of default Newform.aspx page.
In the same way we can change editform.aspx page with our custom webpart by adding the ?&Toolpaneview=2 to the url.
Hope this article would be a help to you.









 

Monday, May 16, 2011

Show Attachments in Custom List Form - Display Form using SharePoint Designer

Basically when you clone the list display form in SharePoint "dispform.aspx" to show up custom list form you will not able to able to see some of the controls like attachments . 


Here is the workaround to achieve this:



To show up attachments just add this line of code using SharePoint designer

<SharePoint:AttachmentsField ControlMode="Display" FieldName="Attachments" runat="server" Visible="true"/>
and this will show you the attachments for the list items exists.

Sunday, May 1, 2011

How to create Sharepoint 2010 Modal Dialog Pop up ( Popup with Faded Background )

Many a times we have a requirement like we need to open a Pop-up with the Faded Background. Sharepoint 2010 has provided us a very simple way to achieve this.
Below is the JavaScript with the help of which we can open a Modal Dailog Pop Up of Sharepoint 2010.
<script type="text/javascript">
     function OpenDialog(URL) {
         var NewPopUp = SP.UI.$create_DialogOptions();
         NewPopUp.url = URL;
         NewPopUp.width = 700;
         NewPopUp.height = 350;
         SP.UI.ModalDialog.showModalDialog(NewPopUp);
     }
 </script>

Call the Javascript on Button click and the modal PopUp will pops up with the Page of Specified URL.
btnOpenPopUp.Attributes.Add("onClick", "javascript:OpenDialog(‘/_layouts/MyAppPage/MyPage.aspx’);");