Search This Blog

Tuesday, October 14, 2014

SharePoint 2010 List Joins/ SharePoint Features/ Features Stapling

SharePoint 2010 List Joins – SPQuery enchancements
Joins
Following ways of joining lists to pull out aggregated data:
§  SharePoint API (SPQuery object)
§  LINQ to SharePoint
§  CAML
§  SharePoint Designer 2010

SPQuery enhancements
Using the object model with SharePoint 2010, you now have the ability to use the properties of the SPQuery object:
§  SPQuery.Joins
§  SPQuery.ProjectedFields
SPQuery.Joins Property
With the new property called Joins on the SPQuery object, we can (using CAML) define a join for our query like so:
A working sample of the SPQuery code could look like this code:
SPQuery query = new SPQuery();
query.Query = “[YOUR CAML QUERY HERE]“;
query.Joins = “[YOUR JOIN CAML HERE (See above for example)]“;
query.ViewFields = “[SAME AS BEFORE]“;
SPListItemCollection items = myList.GetItems(query);
foreach(SPListItem item in items)
// Do with your items here like you’re used to, but now all joined up
So the only thing you really need to do is to make sure you’ve got the join set up with the SPQuery.Joins property – and then trigger
SPQuery.ProjectedFields Property

what is the maximum file size limit in Sharepoint 2013 ?

Its 2GB.
Yes the maximum size of file should not exceed 2 GB.

Handling List with Huge Amount of Data/Items ? -- the Concept of ContentIterator

How to Handle Large list data in SharePoint?
SPQuery Fails for Large Lists Items?
Unable to Retrieve list item from large lists? 
ContentIterator to the rescue...
so how to use it? 
 If indexed column condition return more value than List View Threshold, it handles by batch..
    1-  Simply include the  Microsoft.Office.Server.dll  which is available in 4/ISAPI/
    2 -  Include namespace Microsoft.Office.Server.Utilities. as reference
So what it gives us?? 
· Fetches list items as a batch so it reduces the load.
· Batch Procesing can be stopped at anytime.
· Alternative to SPQuery as it fails for large data greater than list threshold
It works as follows
SPQuery.ListItemCollectionPosition in 2007 MOSS  helps to fetch large number of items efficiently in the batches. ContentIterator.ProcessListItems method make use of SPQuery.ListItemCollectionPosition internally in a such way that its value is less than the list threshold value .
ContentIterator will run through each item in the list, invoking the callback provided for list item processing—in this case, ProcessItem. If an error occurs while iterating the list, then the error function is invoked—in this case, ProcessError. Using this approach the ContentIterator processes the list in pieces and avoids any excessively large queries. This functionality is provided as part of Enterprise Content Management (ECM) in SharePoint Server 2010.
static int exceptions = 0;
static int items = 0;
protected void OnTestContentIterator(object sender, EventArgs args)
{
    items = 0;
    exceptions = 0;
    string query1 = @"<View>
        <Query>
            <Where>
                <And>
                    <BeginsWith>
                        <FieldRef Name='Title' />
                        <Value Type='Text'>A</Value>
                    </BeginsWith>
                </And>
            </Where>
        </Query>
    </View>";
    ContentIterator iterator = new ContentIterator();
    SPQuery listQuery = new SPQuery();
    listQuery.Query = query1;
    SPList list = SPContext.Current.Web.Lists["Parts"];
    iterator.ProcessListItems(list,listQuery,ProcessItem,ProcessError)
    );
}
public    bool ProcessError(SPListItem item, Exception e)
{
    // process the error
    exceptions++;
    return true;
}
public void ProcessItem(SPListItem item)
{
    items++;
    Do what you wish with the item
}
How to check if a user belongs to a SharePoint Group or Not ? 
The method is called as  IsCurrentUserMemberOfGroup(GroupName) - it returns a true if the user is present in the Group  else it returns a false ..  Here is the sample code for the same.... 

private bool IsMember()
{
bool
 isMember;
SPSite site =
 new SPSite(SiteURL);
SPWeb web = site.OpenWeb();

//Returns true on successfull membership 

isMember = web.IsCurrentUserMemberOfGroup(web.Groups["GroupName"].ID);

web.Close();
site.Close();
return
 isMember;
}

How to Retrieve the user name from Person or Group Field(People Finder) in SharePoint

There are two classes used here
1. SPFieldUser -- Gets the field or column onformation
2. SPFieldUserValue- -Gets the values from SPUSERFIELD

Export a List in SPS2010

Navigate to Central Admin--> backup and Recovery -->  Export a list
and Perform the following : Click on the Image to see a  better View

 


 


What is a SharePoint Feature? What files are used to define a feature?
A SharePoint Feature is a functional component that can be activated and deactivate at various scopes throughout a SharePoint instances.
Scopes include:-
1.     Farm
2.     WebApplication
3.     Site (site collection)
4.     Web (site)
Features have their own receiver architecture, which allow you to trap events such as when a feature is 
1.     installing
2.     uninstalling
3.     activated
4.     deactivated
The element types that can be defined by a feature include 
1.     menu commands
2.     link commands
3.     page templates
4.     page instances
5.     list definitions
6.     list instances
7.     event handlers
8.     workflows


The two files that are used to define a feature are 
1-feature.xml- The actual feature and will make SharePoint aware of the installed feature.
2-manifest file(elements.xml)- The manifest file contains details about the feature such as functionality.
Common stsadm commands associated with feature are
1.     stsadm -o installfeature/ stsadm -o uninstallfeature
2.     stsadm -o activatefeature/ stsadm -o deactivatefeature

SharePoint 2010 - Features Stapling

Feature stapling allows a Feature to be stapled to any new sites created from any site definition or from specific site definitions based on the template name identified in the appropriate WEBTEMP.xml file. The stapled feature will be automatically activated each time a new site or sub-site is created using the associated site definition.

Warning: You can not use feature stapling on a site created using a site template (.stp) file.  If you try to staple feature on a site which was created using site template (.stp) file, the feature will not get activated automatically when the new site is created.

You can staple a feature to a site created using any of the in-built site definition templates or your own custom site definitions. For instance, following are some of the main site definitions available in SharePoint.
Template Name
Template Title
STS#0       
Team Site
STS#1      
Blank Site
STS#2      
Document Workspace
MPS#0      
Basic Meeting Workspace
MPS#1      
Blank Meeting Workspace
MPS#2      
Decision Meeting Workspace
MPS#3      
Social Meeting Workspace
MPS#4      
Multipage Meeting Workspace
WIKI#0     
Wiki Site
BLOG#0     
Blog Site
Feature stapling Components
Feature stapling consists of 3 main components:
1) Stapler Feature: The parent feature which has a higher scope than the Staplee or Child feature.
2) Staplee Feature: The Child feature which gets activated when the Stapler feature gets activated.
3) Stapler Feature's Element.xml: Consist the association between the Staplee feature's Id and the Site definition template name (eg: STS#0) on which it will get activated.
<FeatureSiteTemplateAssociation Id="b94f446e-0989-4d90-8abb-7aa0704f4506" TemplateName="STS#0">

Feature stapling Scenario
We need to create multiple sub sites inside a site collection with template as team site. Each time a new sub-site is created, it must have a list named Team Docs which is automatically created at the time of creation.

Feature stapling Implementation
We can use Feature Stapling in this scenario as follows:
1) Create a Stapler feature scoped to Site level.
2) Create a Staplee feature scoped to the Web level. Also add a feature reciever and write the code for creating a new list named "Team Docs" at the time of activation.
3) Associate the Staplee feature with the Team Site template Name ("STS#0") in the Elements.xml file.
4) Deploy the solution and Install both the features.
5) Activate the Site collection level Stapler feature. The Web level Staplee feature will be automatically stapled.
6) Whenever a new sub-site based on Team Site template is created, a list named Team Docs will be automatically created.

Feature stapling Demo
Following are the steps:-

Create an empty SharePoint 2010 project
Enter your site collection url and select "Deploy as a farm solution" option.

Add a new feature named "FeatureStaplingDemo Stapler" with scope as Site.

Add another new feature named "FeatureStaplingDemo Staplee" with scope as Web.
Now, right click on project and add an Empty Element to the project.

Go to web scoped Staplee feature and copy the feature id from the feature manifest.

Add the following code in the FeatureSiteTemplateAssociation tag in Elements.xml where
id is the feature id of the staplee and
TemplateName is the name of Site template on which we are stapling this feature.

Code for Elements.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="b94f446e-0989-4d90-8abb-7aa0704f4506" TemplateName="STS#0"></FeatureSiteTemplateAssociation>
</Elements>
Now, add a feature receiver to the web scoped Staplee feature.

In the feature activated event, add the code for adding a new list named "Team Docs" whenever a new sub-site is created.

Code for Feature Activated and Deactivating
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
                SPWeb web = properties.Feature.Parent as SPWeb;
                try
                {
                    SPList targetList = web.Lists["Sales"];
                }
                catch (ArgumentException)
                {
                    Guid listId = web.Lists.Add("Team Docs""Team Documents"SPListTemplateType.GenericList);
                    SPList list = web.Lists[listId];
                    list.OnQuickLaunch = true;
                    list.Update();
                }                
        }       
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            try
            {
                SPList list = web.Lists["Team Docs"];
                list.Delete();
            }
            catch (ArgumentException)
            {
            }    

        }

Deploy the solution

In the site collection, go to Site Setting->Site Collection Features. You can see that the Stapler feature has been activated.

Now, create a new sub-site with template as Team Site.

You will notice that the List named "Team Docs" has been automatically created.

Go back to Site Setting->Site Collection Features in the site collection and deactivate the Stapler feature.

Create another sub-site with the template as Team Site.

This time the list named "Team Docs" is not created since the Stapler feature is deactivated.



No comments:

Post a Comment