Pages

Search This Blog

Thursday, December 30, 2010

Customize Default Sharepoint 2010 Application Pages

Have u ever wondered if its possible to customise any of the sharepoint default application pages that reside in "_Layouts" hive as per your business requirement. I will write some detailed steps to accomplish this.

Let us consider a business requirement which says that user should not be able to checkin the major version of a document while checking in a document.

Now since, when user checks in a document, sharepoint opens up "checkin.aspx" page, we will copy the page and customize it since we cannot customise the original page (that will effect whole of the farm). Rename the copied page to "customCheckin.aspx".

Open the custom page and find the tag " and replace it to ""
In the similar way find the tag "{asp:placeholder id="CheckinOverWriteOption" runat="server"}" and replace it with "{asp:placeholder id="CheckinOverWriteOption" runat="server" visible="False"}"

Note: You can customize the page as per your requirement.

Now the issue it to show this page instead of default checkin page whenever user performs checkin, so the easiest solution would be to add a "mapping Url" entry in Web.Config of your application. To do this, open your web.config file and add below lines under "{system.web}" tag.

{urlMappings enabled="true"}
{add url="~/_layouts/checkin.aspx" mappedUrl="~/_layouts/customCheckin.aspx" /}
{/urlMappings}



Now open your site and try to checkin a document, and you will see your checkin page instead of default. (the url will still remains the same in browser as _/layouts/checkin.aspx" but the page will be yours).

Friday, December 24, 2010

Calling Sharepoint Web Service through Javascript

Sharepoint 2010 has introduced Client Object Modal by which you can send a call to Sharepoint client API through code or through Javascript. But if you are calling the client API through Javascript, you can only do so by using asynchronous (executeQueryAsync) call to the native method.
Many a times you may need to get the data in the same method without leaving the control (means by a synchronous call without leaving the method). In that case, the best possible solution would be to use Web service calls.
Below is the code to accomplish this. For the sake of simplicity, I have called "Lists.asmx" web service to get an item by Id.

//Main Method
function GetItemByIdAndTitle(rootSiteUrl, ListTitle, itemId) {
var responseXML;
var ws;
try {
ws = InstanciateHttpRequest();
if (ws != null)
{
var soap = GetEnvelope(ListTitle, itemId); // create a web service envelope here
ws.open("POST", rootSiteUrl + "/_vti_bin/lists.asmx", false);
ws.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
ws.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
ws.send(soap);
if (ws.readyState == '4' && ws.status == '200')
{
responseXML = ws.responseXML.xml;
//you can parse your XML here to get the data
}
}
}
catch (e)
{ }
return responseXML;
}


//This ensures the the object instanciation is supported by multiple browsers // (Mozilla, IE).
function InstanciateHttpRequest()
{
var Obj;
if (window.XMLHttpRequest)
{
Obj = new XMLHttpRequest();
}
else
{
Obj = new ActiveXObject("Microsoft.XMLHTTP");
}
return Obj;
}

//Soap Envelope

function GetEnvelope(listName,itemId)
<
var soap = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<soap:Body>" +
"<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>"+
"<listName>" + listName + "</listName>"+
"<query><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemId + "</Value></Eq></Where></Query></query>" +
"<viewFields><ViewFields><FieldRef Name='ContentType' /></ViewFields></viewFields>" +
"<rowLimit>1</rowLimit>" +
"<queryOptions><QueryOptions><ViewAttributes Scope='Recursive'></ViewAttributes></QueryOptions></queryOptions>" +
"</GetListItems>"+
"</soap:Body>" +
"</soap:Envelope>";
return soap;
>


You can change the soap envelope to get different data by calling other Sharepoint Web services.

Tuesday, December 21, 2010

Context sensitive ECB menu items in sharepoint using javascript.

This is a nice post showing how to build custom ECB menu items that are context sensitive i.e. menus that change according to the metadata of the document.

Sharepoint changes the ECB menu for different states of the document . For eg. when document is checked out, checked in etc. This post provides insight into how to implement similar kind of functionality for custom menu items:

http://weblogs.asp.net/jan/archive/2009/09/11/customizing-the-sharepoint-ecb-with-javascript-part-3.aspx

Monday, December 20, 2010

Custom error mode setting for layout pages.

To check the error in layout pages of 14 hive, if you just try to set the customerrormode = "off" in web.config. It doesn't work.

Layout folder in 14 hive contains one more we.config file and overwrite the customerrormode entry of app config .

To check the error in layout pages, set the customerrormode="Off" in the web.config of layout folder.


Saturday, December 11, 2010

SharePoint : Auditing

Many times we want to track which users viewed and updated records and documents, and when these events occurred. To accomplish this task sharepoint provide support for auditing and include auditable events such as viewing and updating. Sharepoint provides built-in audit logging you can enable and configure at the scope of a site collection. When we enable auditing, Windows SharePoint Services writes audit event entries into an internal audit log table that is stored within the content database.
Windows SharePoint Services does not provide the functionality to see entries that are written to the audit log, a developer must write code to enable the windows sharepoint services audit logging facility. A developer must supply code and a user interface to read entries from the audit log and display this data to the site's users. But yes SharePoint Server provides a reporting function that uses Microsoft Office Excel workbooks to display and keep records of audit logs.
Enable site collection level auditing:
SPSite objSiteCollection = (SPSite)properties.Feature.Parent;
objSiteCollection.Audit.AuditFlags = SPAuditMaskType.All;
objSiteCollection.Audit.Update();
You can use bitwise logical operators to combine multiple SPAuditMaskType flags.
For example:
SPAuditMaskType objSPAuditMaskType= new SPAuditMaskType();
objSPAuditMaskType = SPAuditMaskType.View | SPAuditMaskType.Delete | SPAuditMaskType.CheckIn;
 
Enable list level auditing
using (SPSite objSiteCollection = new SPSite("http://test")) {
  using (SPWeb site = objSiteCollection.OpenWeb()) {
    SPList list = site.Lists["XYZ"];
    list.Audit.AuditFlags = SPAuditMaskType.All;
    list.Audit.Update();
  }
}
Enable auditing information per item basis:
To accomplish this we are required to create a menu item (Item Audit History) within the ECB for all documents within the site collection where the item auditing feature is activated. TO do it create a custom action as below:
<CustomAction Id="AuditingFeature.ECBItemMenu"
  RegistrationType="List" 
  RegistrationId="101"
  ImageUrl="/_layouts/images/ECB/GORTL.GIF"
  Location="EditControlBlock"
  Sequence="400"
  Title="Item Audit History">
    <UrlAction Url="~testSite/_layouts/AuditHistoryPage.aspx?ItemId={itemId}&ListId={listId}"/>
CustomAction> 
Now we are required to code AuditHistoryPage.aspx page that will be used to display the item audit history. You can refer the below code:
SPSite objSPSite = SPContext.Current.Site;
SPWeb objSPWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()  {
  using (SPSite objSiteCollection = new SPSite(objSPSite.ID)) {
    using (SPWeb objSite = objSiteCollection.OpenWeb(objSPWeb.ID)) {
      SPList objSPList = objSite.Lists[new Guid(listId)];
      SPListItem objSPListItem = objSPList.Items.GetItemById(Convert.ToInt32(itemId));
      SPAuditQuery objSPAuditQuery;
      SPAuditEntryCollection objSPAuditEntryCollection;
      objSPAuditQuery = new SPAuditQuery(objSiteCollection);
      objSPAuditQuery.RestrictToListItem(objSPListItem);            
      objSPAuditEntryCollection = objSite.Audit.GetEntries(objSPAuditQuery);
      foreach (SPAuditEntry objSPAuditEntry in objSPAuditEntryCollection)  {
        // get all audit entry…
      }
    }
  }
});
Thanks

Wednesday, December 8, 2010

Friday, December 3, 2010

Code & Quality Check

For SharePoint Code quality one should always use these two tools :

SharePoint Dispose Checker Tool


SPDisposeCheck is a tool to help you to check your assemblies that use the SharePoint API so that you can build better code. It provides assistance in correctly disposing of certain SharePoint objects to help you follow published best practice. This tool may not show all memory leaks in your code. Further investigation is advised if you continue to experience issues.

FxCop
FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements. Many of the issues concern violations of the programming and design rules set forth in the Design Guidelines, which are the Microsoft guidelines for writing robust and easily maintainable code by using the .NET Framework.




SharePoint 2010 Best Practices

Wednesday, December 1, 2010

SharePoint Facebook WebParts

Have you ever needed to show the facebook updates of your organization on your sharepoint portal ? If yes, this project is for you.

What we provide is two webparts for SharePoint , one that can be used to show the facebook wall of any user on the SharePoint portal. Second webpart can be used to post updates to facebook directly from your sharepoint portal. Just download and install, and you are all set


SharePoint Twitter WebParts

Are you looking to show the twitter updates of your organization on your sharepoint portal ?

If yes, then we are providing two webparts for SharePoint 2007 & SharePoint 2010, one that can be used to show the tweets of any user on the SharePoint portal. Second webpart can be used to post tweet to twitter directly from your sharepoint portal. Just download and install, and you are all set. All you need is to configure the twitter settings after installation.

These web parts use out of the box SharePoint CSS elements that match automatically with your current site theme.