Dec 15, 2012

Creating List Programatically in Sharepoint 2010 using Feature Receiver

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Devendra_Feature_Event_Receivers.Features.AddList
{
    /// 
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// 
    /// 
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// 

    [Guid("5bf5f6c5-c371-419a-984b-f0ecf9ddeda0")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                //get the current web object.
                SPWeb web = properties.Feature.Parent as SPWeb;

                //Get the list object. this method is used to create the list if list already exist then it will return the list object.
                SPList list = web.EnsureList("Employee", "This list contains Employee details.", SPListTemplateType.GenericList);

                //Check if list exist.
                if (list != null)
                {
                    //Rename Title Field as FirstName.
                    SPField field = list.EnsureField("Title", "", SPFieldType.Text, true);
                    field.Title = "FirstName";
                    field.Update();

                    //Add the filed to the list.
                    SPField fld = null;
                    //Add the filed to the list.if field already exist in the list then it will return the object of the field.
                    fld = list.EnsureField("LastName", "Employee LastName", SPFieldType.Text, true);

                    //Add field to the default view.
                    if (fld != null)
                    {
                        SPView view = list.Views[0];
                        view.ViewFields.Add(fld);
                        view.Update();
                        fld = null;
                    }
                    //Add values to the list
                    SPListItemCollection listItems = list.Items;
                    AddItems(listItems, "Devendra", "Velegandla");
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void AddItems(SPListItemCollection listItems, string FirstName, string LastName)
        {
            SPListItem item = null;
            item = listItems.Add();
            item["FirstName"] = FirstName;
            item["LastName"] = LastName;
            item.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //Get the current web object.
            SPWeb web = properties.Feature.Parent as SPWeb;
            //Ensure and delete the list.
            web.DeleteList("Employee");
        }



    }

}




add one more class called utilities.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Devendra_Feature_Event_Receivers.Features.AddList
{
public static class Utilities
{
/// Used to create the list by using the list template type.
public static SPList EnsureList(this SPWeb web, string listTitle, string desc, SPListTemplateType lstTemplateType)
{
//Get the list collection.
SPListCollection lstCollection = web.Lists;

//Check If List already Exist in the system.
SPList lstObj = (from SPList lst in lstCollection
where string.Equals(lst.Title, listTitle, StringComparison.InvariantCultureIgnoreCase) == true
select lst).FirstOrDefault();

//If yes return the list object.
if (lstObj != null)
{
return lstObj;
}
//List does not exist and need to create a new one
Guid lstGuid = web.Lists.Add(listTitle, desc, lstTemplateType);
try
{
SPList newList = web.Lists.GetList(lstGuid, true);
newList.OnQuickLaunch = true;
newList.Update();
return newList;
}
catch //If for some reason list creation fails returns an null object
{
return null;
}
}
/// Used to create the list column with display name and field type
public static SPField EnsureField(this SPList list, string fldDisplayName, string fldDesc, SPFieldType fldType, bool isMadatory)
{
//get list the field collections
SPFieldCollection fieldCollection = list.Fields;

//Check If field already exist in the list.
SPField spField = (from SPField field in fieldCollection
where string.Equals(field.Title, fldDisplayName, StringComparison.InvariantCultureIgnoreCase) == true
select field).FirstOrDefault();

//If exist return the field.
if (spField != null)
{
return spField;
}
//If field does not exist Add the field to the list
try
{
list.Fields.Add(fldDisplayName, fldType, isMadatory);
SPField spfield = list.Fields.GetField(fldDisplayName);
spfield.Description = fldDesc;
spfield.Update();
return spfield;
}
catch // Incase if any exception occur.
{
return null;
}
}
/// Used to Ensure that subscription list exist and if yes then delete the list.
public static bool DeleteList(this SPWeb web, string listTitle)
{
//Get the list collection.
SPListCollection lstCollection = web.Lists;

//Check If List Exist in the system.
SPList lstObj = (from SPList lst in lstCollection
where
string.Equals(lst.Title, listTitle, StringComparison.InvariantCultureIgnoreCase) == true
select lst).FirstOrDefault();

//If yes delete the list object.
if (lstObj != null)
{
try
{
lstObj.Delete();
return true;
}
catch (Exception ex)
{
}
}
return false;
}
}
}



SharePoint-Journey for Administrators, Developers and users

Seqential workflow in SharePoint 2010 using Visual studio 2010


Hello guys,

In this article we will work on developing sequential workflow in SharePoint 2010 using Visual studio 2010.
let start creating new project of type Sequential Workflow ,under SharePoint 2010 in Visual studio 2010.


Deploy as form Solution. Click Next

Name workflow as devendra Sequential -workflow and select site workflow.
click on Next

CLick Next.

CLick Next.


Click on Finish.
It will show the below project structure.

Go to tool box. Select Create Task under SharePoint workflow

Drag and drop below onworkflowActivated .

Click on the redmark in Createtask1 control

Click on Activity ‘createTask1’ does not have Correlation property set.
Give the Correlation token as Task token.
Select Owner ActivityName as Workflow1.

Click on TaskProperties.Select Bind to new member.
Select create Field. Click OK.

Go to workflow.cs[design] > right click > View Code
It will redirect to the code file workflow.cs file you can see the filed you have added just now.
If you check the Bind to existing member you can the Field added it.


Click on TaskID from the properties of CreateTask activity. Select Bind to new member.
Select create Field. Click Ok.
Go to workflow.cs[desgin]right click View Code
It will redirect to the code file workflow.cs file you can see the filed you have added just now.

Go to Workflow1.cs-Desgin.
Double click on Create Task Activity.It will generate a method called createTask1_MethodInvoking.
Write the below code in the method.

        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {
            createTask1_TaskId1 = Guid.NewGuid();

            createTask1_TaskProperties1.Title = "New Task";
            createTask1_TaskProperties1.Description = "Please complete this ASAP";
            createTask1_TaskProperties1.AssignedTo = workflowProperties.Originator;

        }

The above code create the task for user we runs the workflow.
Go to ToolBox select LogtoHistoryListActivity under Sharepoint workflow

Drag and drop the LogtoHistoryListActivity to workflow.cs[design] file below create task

Click on logToHistoryActivity1 Press F4 to see the properties.
Click on History Description.

SharePoint-Journey for Administrators, Developers and users

Nov 23, 2012

Project structure is for SharePoint Hosted Apps in Visual studio 2012 and AppManifest.xml



Hi Guys,
Today we will see what the Project structure is for SharePoint Hosted Apps in Visual studio 2012 and significance of AppManifest.xml file.





·         Features: This folder is used to deploy features to the server that's running SharePoint.
·         Package: This folder is used to deploy the solution to the server that's running SharePoint.
·          Images: This folder is used in the app for SharePoint project, such as icons or screenshots.
·          Pages: This folder contains .aspx or .html pages that are used in the app for SharePoint project.
·         Scripts: This folder contains any JavaScript code files that are used in the app for SharePoint project.
·          Content: This folder contains any .css style sheets that are used in the app for SharePoint project.
·         AppManifest.xml: Defines the various elements of the app for SharePoint project.



AppManifest.xml:

AppManifest.xml is an important file in the project, which describes various properties of App and how it behaves in SharePoint 2013. The properties are Title, Start page, Permissions, Feature prerequisites, capability prerequisites.




If you open the AppManifest.xml by Right click on the file and Open with XML (Text) Editor.



You can see the details of configuration in XML editor format.





The above screen “Name” property is Name of the SharePoint App.
Product Id” is Unique Identifier of the SharePoint App, it doesn’t change with the versions.
Version” is version of the SharePoint App.
SharePointminimumversion” is the minimum version of the SharePoint with which the app compatible with.
“AppPermissionRequests”   this specifies what are all the resources required by the app.





In the above screenshot shows the list of scope that SharePoint hosted App can support, different scopes have different levels of permissions. Example BCS will have only Read and Write Permissions.
When you deploy the App to SharePoint by setting the permissions it will ask the user who is deploying it to SharePoint “DO you trust ‘Appname’”.  User can select “Trust” or Cancel. If Trust is selected it will get added to the SharePoint hosted site



AppPrerequisites property can add list of features and capabilities that are required to support the App.

SharePoint-Journey for Administrators, Developers and users