Sunday, 6 January 2013

Event Handler Code


        //writing the note in the notepad
        public void writeToNotepad(string strTxt)
        {
            StreamWriter strWriter = File.AppendText(@"c:\Hello.txt");
            strWriter.Write(strTxt + "\n");
            strWriter.Close();
        }
        //Db information
        public void saveData(string strUserName, string dtTime, string strDesc)
        {
            //dtTime = DateTime.Now.ToString() ;
            SqlConnection con = new SqlConnection("server=madan;uid=sa;pwd=madan@123;database=mydb");
            string sqlStr = "insert into allevents values('" + strUserName + "','" + dtTime + "','" + strDesc + "')";
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                con.Close();
            }
        }
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           writeToNotepad("Item Adding :" + properties.ListItem.Title + " Adding time " + DateTime.Now.ToString());
           saveData("Madan", DateTime.Now.ToString(), "Item adding");
           //saveData("Madan", DateTime.Now.ToString(), "Iteam Adding");
       }
       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           writeToNotepad("Item Updating :" + properties.ListItem.Title + " Updating time " + DateTime.Now.ToString());
           saveData("Madan", DateTime.Now.ToString(), "Item Updating");
       }
       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           properties.Cancel = true;
           properties.ErrorMessage = "Evening batch you dont have the delete permissions..";
       }
       /// <summary>
       /// An item is being checked in.
       /// </summary>
       public override void ItemCheckingIn(SPItemEventProperties properties)
       {
           base.ItemCheckingIn(properties);
       }
       /// <summary>
       /// An item is being checked out.
       /// </summary>
       public override void ItemCheckingOut(SPItemEventProperties properties)
       {
           base.ItemCheckingOut(properties);
       }
       /// <summary>
       /// A file is being moved.
       /// </summary>
       public override void ItemFileMoving(SPItemEventProperties properties)
       {
           base.ItemFileMoving(properties);
       }
       public override void ItemDeleted(SPItemEventProperties properties)
       {
           base.ItemDeleted(properties);
       }

Saturday, 5 January 2013

Create a Custom Feature in Site Settings Multiple Group Locations

  1. Go to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES physical location
  2. Create a folder (Ex: ManageLinks)
  3. Create the following two files

                   1. Feature.xml
                   2. Elements.xml

    4.   In  Feature.xml   Write the following code

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="2294626A-735B-4CA7-B371-11848CA40348"
         Description="Manage eMail Links"
         Hidden="False"
         Title="Manage eMails"
         Scope="Web" >
   
    <ElementManifests>
        <ElementManifest Location="Elements.xml" />
    </ElementManifests>
</Feature>
        

    5. In Elements.xml Write the following code

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
    <CustomAction Id="B8EA2F40-61A9-4685-9CCE-7CC4D8612B0C"
                  Title="MSN"
                  GroupId="Galleries"
                  Description="Redirecting Hotmail Account"
                  Location="Microsoft.SharePoint.SiteSettings"
                  Sequence="0">
        <UrlAction Url="http://www.microsft.com"/>

    </CustomAction>

    <CustomAction Id="88E02BDD-A84A-47B3-8335-7BD2F16A11A3"
                  Title="Yahoo"
                  GroupId="Customization"
                  Description="Redirecting Yahoo Account"
                  Location="Microsoft.SharePoint.SiteSettings"
                  Sequence="0">
        <UrlAction Url="http://www.Yahoo.com"/>

    </CustomAction>

    <CustomAction Id="158D92D0-4C0C-4B6B-82EC-9DD92C2F4BC5"
                  Title="Gmail"
                  GroupId="SiteCollectionAdmin"
                  Description="Redirecting Gmail Account"
                  Location="Microsoft.SharePoint.SiteSettings"
                  Sequence="0">
        <UrlAction Url="http://www.gmail.com" />

    </CustomAction>

</Elements>

6.  Now go to command prompt in server . of the following location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Bin

Use the following command to Install the Feature using STSADM.

stsadm -o installfeature -name "ManageLinks"

You will get operation completed message.

Now go site home page, Click on Site Action, Click on Site Settings.


Under Site Actions, Click on Manage Site Features

Find ManageLinks Feature and Click on Activate Button.



Monday, 8 October 2012

Client Side Code Samples

Equivalent Objects in Server and Client OMs







SERVER OM
.NET MANAGED
ECMASCRIPT
Microsoft.SharePoint.SPContext
Microsoft.SharePoint.Client.ClientContext
SP.ClientContext
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.Client.Site
SP.Site
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.Client.Web
SP.Web
Microsoft.SharePoint.SPList
Microsoft.SharePoint.Client.List
SP.List
Microsoft.SharePoint.SPListItem
Microsoft.SharePoint.Client.ListItem
SP.ListItem
Microsoft.SharePoint.SPField
Microsoft.SharePoint.Client.Field
SP.Field
Which DLLs Implement the Client OM
Client OM located under  %Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI .
There are two DLLs for the managed OM, Microsoft.SharePoint.Client and Microsoft .SharePoint.Client.Runtime .
Add references to VS.
Ex:1
Retrieving site titles and its Urls
ClientContext context = new Microsoft.SharePoint.Client.ClientContext("http://Mohan:8089");
Web site = context.Web;
context.Load(site);
context.ExecuteQuery();
MessageBox.Show("Title: " + site.Title + " Relative URL: " + site.ServerRelativeUrl);
context.Dispose();
List Collections
ListCollection lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();
MessageBox.Show(lists.Count.ToString());
foreach (Microsoft.SharePoint.Client.List list in lists)
{
MessageBox.Show("List: " + list.Title);
}
Ex:2 List the Properties
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
Web site = context.Web;
context.Load(site, s = > s.Title, s = > s.ServerRelativeUrl);
ListCollection lists = site.Lists;
context.Load(lists, ls = > ls.Include(l = > l.Title));
context.ExecuteQuery();
MessageBox.Show("Title: " + site.Title + " Relative URL: " + site.ServerRelativeUrl);
MessageBox.Show(lists.Count.ToString());
foreach (Microsoft.SharePoint.Client.List list in lists)
{
MessageBox.Show("List: " + list.Title);
}
context.Dispose();
Ex:3
Using CAML Query
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
List list = context.Web.Lists.GetByTitle("Announcements");
ListItemCollectionPosition itemPosition = null;
while (true)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml = @"
< View >
< Query >
< Where >
< IsNotNull >
< FieldRef Name='Title' / >
< /IsNotNull >
< /Where >
< /Query >
< RowLimit > 1000 < /RowLimit >
< /View > ";
ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems);
context.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
foreach (ListItem listItem in listItems.ToList())
{
MessageBox.Show("Title: " + listItem["Title"]);
}
if (itemPosition == null)
{
break;
}
MessageBox.Show("Position: " + itemPosition.PagingInfo);
}
Ex:4
Using the LINQ
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
var query = from list
in context.Web.Lists
where list.Title != null
select list;
var result = context.LoadQuery(query);
context.ExecuteQuery();
foreach (List list in result)
{
MessageBox.Show("Title: " + list.Title);
}
context.Dispose();
Ex:-5
Creating Lists, Fields, and Items
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
Web site = context.Web;
ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "New List";
listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
List list = site.Lists.Add(listCreationInfo);
Field newField = list.Fields.AddFieldAsXml(@" < Field Type='Text' DisplayName='NewTextField' > < /Field > ", true, AddFieldOptions.AddToDefaultContentType);
ListItemCreationInformation itemCreationinfo = new ListItemCreationInformation();
ListItem item = list.AddItem(itemCreationinfo);
item["Title"] = "My New Item";
item["NewTextField"] = "My Text";
item.Update();
context.ExecuteQuery();
context.Dispose();
Ex:-6
Deleting Lists and Items
ClientContext context = new Microsoft.SharePoint.Client. ClientContext("http://Mohan:8089");
List list = context.Web.Lists.GetByTitle("New List");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"
< View >
< Query >
< Where >
< IsNotNull >
< FieldRef Name='Title' / >
< /IsNotNull >
< /Where >
< /Query >
< /View > ";
ListItemCollection listItems = list.GetItems(camlQuery);
context.Load(listItems, items = > items.Include(item = > item["Title"]));
context.ExecuteQuery();
foreach (ListItem listItem in listItems.ToList())
{
listItem.DeleteObject();
}
context.ExecuteQuery();
context.Dispose();
Advantages
Less Deployment Hassles: Using Client OM, you do not need to install the components required by the Server Object Model. Thus Client OM provides much ease to the end user.
Language Flexibility: We can use the following languages to work with the Client OM:
Microsoft .NET
Silverlight
ECMA Script (JavaScript /JScript)
Query Speed Optimizations: In the Client OM, reduced network traffic is attained using Query Optimizations. Thus the user will feel reduced round trips and other advantages like paged results, etc.


Thursday, 6 September 2012

Feature Using the Custom Action Pages

In the above example (http://www.blogger.com/blogger.g?blogID=403610387301840191#editor/target=post;postID=5161020807956575523) I used the public site URL Action elements. In this example use own ASPX pages as target.
For this feature, create a Feature.xml file and Manifest file that adds a new link to the Site Settings Galleries section.
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
            Id="4C652168-BBCE-4B2A-BA02-B66814B272B8"
            Scope="Site"
            Title="Content Type Hierarchy Pages sample"
            Description="Show the Hierarch content type page sample"
            >
     <ElementManifests>
           <ElementManifest Location="Elements.xml"/>
     </ElementManifests>
</Feature>

Create the Elements.Xml file
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <CustomAction Id="9AE49699-675D-4DB8-8082-81C832E52D9B"
                       GroupId="Galleries"
                       Location="Microsoft.SharePoint.SiteSettings"
                       Sequence="0"
                       Title="Site Content type Hierarchy Pages."
                       Description="Display Site content Hierarchy of site content pages.">
           <UrlAction Url="_layouts/TestPage.aspx"/>
     </CustomAction>
</Elements>


Create a TestPage.aspx and place into this location “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS”
Example TestPage.aspx code as follows
<%@ Page Language="C#"  %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% Response.Write("This is my custom page testing .."); %>
    </div>
    </form>
</body>
</html>
Install the feature, and activate the feature à Once you activated the feature, You will find a New link in the Galleries of the top level site settings.
 

Create a Custom Feature

Creating a Custom Feature, Adding a Link in Site Actions

1. Go to the C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\Features Location

2. Add a new folder (ex: - MSDownLoads), with in this folder add two xml files with following code.

1. Feature.xml

<?xml version="1.0" encoding="utf-8" ?>
<
Feature xmlns="http://schemas.microsoft.com/sharepoint/"Id="F023AE13-83FE-4E4C-B011-8F0B47026431"Title="My Custom Feature"Description="Mor batch custome feature to download a ms-softwares"Hidden="FALSE"Scope="Web" Version="1.0.0.0"><
ElementManifests><
ElementManifest Location="Elements.xml"/></
ElementManifests>
</
Feature>
2. Elements.xml
<?xml version="1.0" encoding="utf-8" ?><
Elements xmlns="http://schemas.microsoft.com/sharepoint/"><
CustomAction Id="22BC8409-9429-49D2-A629-CB6AB2159A9B"GroupId="SiteActions"Location="Microsoft.SharePoint.StandardMenu"Title="Downloads" Description="MSdownloads"Sequence="12000"><
UrlAction Url="Http://www.microsoft.com"/>
</
CustomAction> </
Elements>
3. Now go to the command prompt change to this location (C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\bin) and install the feature using with STSADM.EXE

         Ex: STSADM.EXE -o installfeature -foldername(MSDownLoads)
         You will get operation completed successfully

4. Now go to the web application (http://Mohan:8087), click on any sub site, click on Site Actions, Site Settings.

Under Site Actions Section, Click on Manage Site Features, Find
My Custom Feature feature Click on Activate.

Now Click on Site Actions, You will find a Link Downloads, After Site Setting.