Sunday, 10 July 2011

_spBodyOnLoadFunctionNames array


SharePoint pages are the combination of a master page and content pages. Master page contains the “body” element and content pages are used to display the content. Because of this design you can not directly add a function to the body’s onload event.

SharePoint provides the “_spBodyOnLoadFunctionNames” array. When the body is loaded, the onload event handler executes every function whose name is contained in this array.  Example:

To execute “test” method, add this to the array so when body’s onload event fires this method gets executed:

<script language="javascript">   
    _spBodyOnLoadFunctionNames.push("test");   
    function test()   
    {   
        alert(‘hello’);   
    }   
</script> 


Note :
If you wants to execute any function on every page then add this function to master page.

if you wants to execute this function on a specific page then add a content editor web part on the page and put the function in that web part.

Wednesday, 6 July 2011

Get Data from all the folder of a library using SpQuery.ViewAttributes

In recent project we were having the documents in document library organized in different folder structure. We have to show the latest uploaded documents on the homepage using some business logic.

But the problem was, spquery was returning only root level documents.

After googling i found the solution from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewattributes.aspx

Which says the spquery contains ViewAttributes  property and setting it to recursive will do the job.

   
    SPWeb objSpWeb = SPContext.Current.Web)

    SPList objSpList = objSpWeb.Lists["MyLibrary"];
    SPView objSpView = objSpList.Views["MyView"];

    SPQuery objSpQuery = new SPQuery(objSpView);
    objSpQuery.ViewAttributes = "Scope=\"Recursive\"";
    SPListItemCollection objItemColl =  objSpList.GetItems(oQuery);

    foreach (SPListItem objListItem in objItemColl)
    {
        // Perform the required task
    }

SPListItem.Update() SPListItem.SystemUpdate() SPListItem.UpdateOverwriteVersion()



We have different method to update a list item. Following is description of some of available method :

SPListItem.Update() : 

This is the most common method to update the list item. 
This method update the database changes made to list item.

Note : If a document is checkout from a document library and you try to update the metadata, it will throw the exception.


SPListItem.SystemUpdate() :

This method updates the listitem with changes that are made to list item without changing the modified or modified by field.

When the SystemUpdate() method is called, events are triggered, the modifications are reported in the Change and Audit logs, but alerts are not sent and properties are not demoted into documents.

SPListItem.SystemUpdate(Boolean) :

This is useful with lists which have versioning enabled. Pass False to bypass creating a version of the updated item rest it perform same as SytemUpdate().

SPListItem.UpdateOverwriteVersion()

This method updates the current list item without creating another version of the item. Rest is same a SPListItem .Update()


Sunday, 3 July 2011

virtual path provider Sharepoint

A virtual path provider provides a mechanism with which we can extend ASP.NET to serve virtual content to the compilation system. For example, a virtual path provider provides a means to supply content from locations other than the file system.



In other words, it’s an abstract mechanism by which we can render pages from SQL Server content database and also from the virtual directories and from our custom location also we can provide.

So when as a request comes for a page virtual provider provided by SharePoint reads from the content database based on if page is customized and if the page is not customized read from the file system parses it and the passes the page to the ASP.NET run-time.

For more information or to write custom virtual provider
http://support.microsoft.com/kb/910441