Wednesday 28 November 2012

Check the user in a particular group and check it with the current logged in user to hide/disable controls

Below is the script to check the user in a particular group and check it with the current logged in user using JavaScript and Jquery.

If current user is present in desired group than you can implement any of the logic.
I have implemented disabling controls and hiding tables.

ExecuteOrDelayUntilScriptLoaded(disableControls, “sp.js”);
function disableControls()
{
clientContext = new SP.ClientContext();
groupCollection = clientContext.get_web().get_siteGroups();
group = groupCollection.getById(21); //The ID of the SharePoint user group(can be checked in group URL)
users = group.get_users();
clientContext.load(group);
clientContext.load(users);
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
RefreshCommandUI();
}
function onQuerySucceeded()
{
if(users.get_count() >0)
{
UserExistInGroup = false;
for(var i=0; i < users.get_count(); i++)
{
if(users.itemAt(i).get_loginName() == this.currentUser.get_loginName())
{
UserExistInGroup = true;
break;
}
}
}
if (UserExistInGroup)
{
var p=$(‘input[title=col1'); //Name of the columns that needs to be disabled
var i=$('input[title=col2]‘); //Name of the columns that needs to be disabled
$($(p)[0]).attr(‘disabled’,false);
$($(i)[0]).attr(‘disabled’,false);
document.getElementById("Table1").style.display = 'none';
}
else
{
var p=$(‘input[title=col1');
var i=$('input[title=col2');
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
document.getElementById("Table1").style.display = 'inline';
}
}
function onQueryFailed(sender, args)
{
var p=$(‘input[title=col1');
var i=$('input[title=col2]‘);
$($(p)[0]).attr(‘disabled’,true);
$($(i)[0]).attr(‘disabled’,true);
document.getElementById("Table1").style.display = 'inline';
}

Sharepoint Web part life Cycle



Sharepoint Web part life Cycle:

 Below is the steps for life cycle of a web part.

OnInit: This method handles initialization of the control.
protected override void OnInit(EventArgs e)
{
 
}


OnLoad: This event handles the Load event. This is also used for initialize the control but is not intended for loading data or other processing functionality.
protected override void OnLoad(EventArgs e)
{
}

CreateChildControls
  • This is the most popular event in web part life cycle.
  • This creates any child controls. So if you are adding any control to display then you have to write in this method.
  • Event used to set properties and default values to controls collection
  • If the page is being rendered for the first time the method generally occurs after the OnLoad event.
  • If it is Postback, It is called before OnLoad event.
In this routine control properties and methods previously declared are defined. In most cases, we have to initialize the control's default value (such as Text, Checked, Items, and so on) and activity that is possible to call just at first WebPart load, checking PostBack. The controls specified are created and added to the controls collection. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.
protected override void CreateChildControls()
{
}
OnPreRender
  • Event used to set up business logic related work and in this routine controls value kept by viewstate
  • Here we can change any of the web part properties before the control output is drawn. This is the routine where the control's value is kept for the View State.
protected override void OnPreRender(EventArgs e)
{
}
Render
  • HTML Output is generated.
  • Event creates the HTML that appears in the body of the Web Part.
  • The CreateChildControls method is used to add controls to the WebPart, and the RenderContents method is used to tell the page framework how to render the control into HTML to display on a page.
     
protected override void Render(HtmlTextWriter writer)
{
}
OnUnload
This is executed when the web part is unloaded.
protected override void OnUnload(EventArgs e)
{
}

Dispose
This to free the memory.
public override void Dispose()
{
}

Please correct me in case i am wrong in any of the step.

Sharepoint 2010 Interview Questions

Some good interview questions for on sharepoint 2010.


Just google for best possible answers.

Interview questions:
1.       What is the base class of SharePoint webpart?
2.       Which 3 files need to be modified during FBA?
3.       What is custom action in SharePoint list and how to add menu item in a list view?
4.       OOPs concepts, real time example on OOPS some scenarios on OOPS.
5.       Polymorphism, overloading and overriding and differences
6.       What is JSON in JavaScript
7.       What is Jquery
8.       Boxing and unboxing in .net?
9.       What is CTS in .net?
10.   Finalize and dispose functions?
11.   Why Visual web part can’t be deployed on sandbox solutions?
12.   Can we use using keyword in current context?
13.   Best practice of using “Using” keyword in SharePoint
14.   Deployment from one server to another (dev-uat-prod)?
15.   Deploying designer workflows, InfoPath forms, site collections, web applications, solutions, web parts.
Refer: http://mysharepointwork.blogspot.in/2010/02/sharepoint-2010-object-model-backward.html
16.   Search configurations
17.   Moss Migration processes
18.   Lotus notes migration procedure
19.   Back end process of deploying a solutions(Means what steps goes on during deployment like safe control entry in Web.config file etc)
20.   Synchronous and Asynchronous events in sharepoint
21.   What’s new in SP2010 over MOSS
Some good interview questions on object model of sharepoint.

Refer:
http://www.sharepointsecurity.com/sharepoint/sharepoint-development/sharepoint-developer-interview-questions-answers/


Will update some more soon....:)

Tuesday 27 November 2012

SharePoint 2010 - Do not dispose guidance for SharePoint objects

Do not dispose the following listed SharePoint objects explicitly

Remember it’s best practice to regularly review your ULS logs for Dispose() related leaks that have occurred during runtime.. SharePoint Server internally is instrumented to report on Dispose related leaks.
SharePoint  2010:
  • SPContext.Current.Site
  • SPContext.Current.Web
  • SPContext.Site
  • SPContext.Web
  • SPControl.GetContextWeb(..)
  • SPControl.GetContextSite(..)
  • SPFeatureReceiverProperties.Feature.Parent
  • SPItemEventProperties.ListItem.Web
  • SPList.BreakRoleInheritance()
    • Do not call list.ParentWeb.Dispose()
  • SPListEventProperties.Web
  • SPListEventProperties.List.Web
  • SPSite.RootWeb
    • Problems may occur when SPContext.Web has equality to the SPContext.Web.. make sure you dispose of SPSite and it will cleanup sub webs automatically
  • SPSite.LockIssue
  • SPSite.Owner
  • SPSite.SecondaryContact
  • SPWeb.ParentWeb
  • SPWebEventProperties.Web
Changes:
  • Microsoft.SharePoint.WebControls.SiteAdminsitrationSelector.CurrentItem
    • When used with WSS 3.0 you must call Dispose(), with SharePoint Foundation 2010 you don’t.
  • Event Receivers and properties.OpenWeb()
    • WSS 3.0: When you call properties.OpenWeb() the returned SPWeb will need to call Dispose()
    • SharePoint Foundation 2010: Use the newly introduced SPItemEventProperties.Web property instead of SPItemEventProperties.OpenWeb() for better performance and to avoid the need to call Dispose().

Wednesday 7 November 2012

Best Practices for InfoPath SharePoint Applications

A we all know designing complex forms using infopath like adding custom code,rules,condional formating takes too much time to load the form which seems ridiculous to the end user.

Before Designing Infopath forms developer should know best practices to design infopath forms.

Here is a nice article on MSDN site:

Got to "Best Practices for InfoPath SharePoint Applications" on below link

http://msdn.microsoft.com/en-us/library/ff961896.aspx

Thanks Enjoy Infopath Development.. :)


Monday 5 November 2012

Migrate a MOSS 2007 Site to SharePoint 2010 Using Database Attach Detach Method

Migrate a MOSS 2007 Site to SharePoint 2010 Using Database Attach Detach Method

In this article, I'll explain you how to migrate a site to SharePoint 2010 using the database attach detach method.

Challenge:
How can I migrate just the site/site collection I want from MOSS 2007 to SharePoint 2010?
Moving the entire farm to SharePoint 2010 could take a lot of time, bordering on seeming like forever. Sometimes, however, we just want to get the contents of a specific site/site collection up and running on the new platform as soon as possible.
Solutions:
Using the database attach method, we can migrate a site/site collection to SharePoint 2010 following these basic steps:
Step 1: Move the needed site to a new Web application.
Step 2: Copy the content database backup to the SharePoint 2010 SQL server.
Step 3: Mount that content database to the SharePoint 2010 server.
Notes before migrating
  • Always backup your content database just to be on the safe side.
  • The MOSS 2007 server must have SharePoint Service Pack 2 installed.
  • Run the upgrade check program on your MOSS 2007 server,
stsadm -o preupgradecheck
  • Fix any issues reported before moving on to the next step.

Step 1: Move the site to a new web application in MOSS 2007
Let's say we want to migrate the SharePoint site at http://demosite/TeamSite.
  1. First, create a new Web application and blank site collection (for example: http://demosite:1111).
  2. Login to the SharePoint server using an account that has the following rights/permissions:
    • SharePoint Farm Admin.
    • Local Administration rights on the Web server.
    • db_owner permission on the content databases.
    • Make sure the access account has access to the site content (sometimes farm admin might not be a site collection admin).
  3. Open Command Prompt and change directory to %COMMONPROGRAMFILES%\Microsoft shared\web server extensions\12\bin, then run the following STSADM commands:
 stsadm -o export -url <URL name> -filename <export file name> -includeusersecurity

stsadm -o import -filename <import file name> -url <URL name> -includeusersecurity


   

The imported Web application might look like this:         


Step 2: Copy the content database backup to the SharePoint 2010 SQL server
  1. Open Microsoft SQL Server Management Studio. Right-click on the content database and select Tasks > Backup...\
  2. Enter the Backup Name, select Destination, and click OK to start.
  3. Copy this backup file to the SharePoint 2010 SQL server.

Step 3: Mount the content database to the SharePoint 2010 server
  1. On the SharePoint 2010 server, create a new empty Web application. For example: http://demosite2010:1111.
  2. If you have any custom components on the original site, install them to this Web application now.
  3. After that, we can restore the MOSS 2007 backup to SharePoint 2010 SQL Server. Open Microsoft SQL Server Management Studio. Right-click on Databases and select Restore Database...


  1. Enter the new database name, making sure that you don't overwrite the original content database. Select the Source and click OK to start.

  1. Open SharePoint 2010 Management Shell and dismount the current SP2010 content database.Dismount-SPContentDatabase <ContentdBName>
  1. Verify that we have all the needed custom components. If there are any errors, make sure you correct them before mounting the SP2007 content database.Test-SPContentDatabase -Name <DatabaseName> -WebApplication <URL>
  2. My test shows no error, so we are ready to run the Mount-SPContentDatabase cmdlet. This cmdlet also upgrades the SP2007 content to SP2010.Mount-SPContentDatabase <ContentDb> -WebApplication http://SiteName
  3. After this process has completed, you need to review the log files (the upgraded log file and error log file) to make sure there are no errors. The log files are located at %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\14\LOGS. The logs are named in the following format: Upgrade-YYYYMMDD-HHMMSS-SSS-error.log and Upgrade-YYYYMMDD-HHMMSS-SSS.log, where YYYYMMDD is the date and HHMMSS-SSS is the time (hours in 24-hour clock format, minutes, seconds, and milliseconds). If there are errors at this time, they will most likely be due to some customization done on the MOSS 2007 server.

We have finally finished the migration process. Notice that after our upgrade is completed, the new site looks exactly like it was in MOSS 2007. In SharePoint 2010, there is a feature called Visual Upgrade that gives you an option to switch between MOSS 2007 look and feel or the SharePoint 2010 view.  Note that if you have customized themes on the old site, you must re-create the new theme on the new site before you can switch.  To switch over to the new SharePoint 2010 look, do this:          
  • Login as Site Owner and select Site Actions > Visual Upgrade.


  • Select Update the user interface. Click OK.

 Here is the SharePoint 2010 look and feel:


Notes:

  • The database attach method works with both SharePoint 2010 Foundation and Server.
  • Limitations of the export/import method are:
    • You cannot save a workflow, alerts, features, solutions, or Recycle-Bin state by using the export operation.
    • Stsadm only supports moving entire Web sites (SPWeb instances). You are not able to export selected items or lists.
    • You cannot retain object globally unique identifiers (GUIDs) when you migrate site objects. New identifiers are added at the migration target.
    • You can experience problems if you rerun Stsadm on site content that has already been imported. If that occurs, all list items that are not document libraries are duplicated on the target site.
    • Other minor limitations are imposed by operation parameters. For example, although the migration object model provides about 25 export/import parameters, the equivalent operations in Stsadm provide only about 10. And in some cases parameters provided in Stsadm are somewhat limited. For example, when applying security settings, Stsadm offers only two options: "no security" or "all security." You should become thoroughly familiar with the Stsadm operations and their parameters before using it for content migration

Friday 2 November 2012

Hide Controls for particular sharepoint group using Jquery

This blog is to hide sharepoint contols, buttons, tables etc. for sharepoint group on page load.


Using CEWP we can achieve this simply refering some Jquery files and add CEWP in your page or list form and give your control id you want to hide and you can hide easily on Page Load.


Steps:

Download Jquery files:
1. jquery.min.js
2. jquery-1.4.2.min.js
3. jquery.SPServices-0.5.4.min.js
4. jquery.SPServices-0.6.2.js

Add the script in your page either using CEWP or directly and refer above mentioned files as below:

<script src="site url/SiteAssets/jquery.min.js" type="text/javascript"></script><script src="/site url/SiteAssets/jquery-1.4.2.min.js" type="text/javascript"></script><script language="javascript" src="/site url/SiteAssets/jquery.SPServices-0.5.4.min.js" type="text/javascript"></script><script language="javascript" src="/site url/SiteAssets/jquery.SPServices-0.6.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
       //alert(xData.responseXML.xml);
        if($(xData.responseXML).find("Group[Name='Group Name']").length == 1)
        {
         //alert('User in a group');
         document.getElementById("Ur control or button id").style.visibility="visible";
      document.getElementById("Ur control or button id").style.display="inline";
        }
        else
        {
         //alert('User not in a group');
         document.getElementById("Ur control or button id").style.visibility="hidden";
         document.getElementById("Ur control or button id").style.display="none";
 
        }
      }
   });
}); </script>



Done!