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.

Wednesday, 5 September 2012

Add the user to a Group

SPSite site = new SPSite(@"http://madhanpc:8089");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        //Get the group that we want to add the user
        SPGroup grp = web.Groups["Viewers"];
        try
        {
            //Get the user that we want to add to the group
            SPUser usr = web.AllUsers[@"madhanpc\emp1"];
            //Now we add the user to the group collection and update
            grp.AddUser(usr);
            grp.Update();
            Response.Write("User is added successfully..");
        }
        catch (Exception ex)
        {
            Response.Write("User is not added " + ex.Message);
        }

Display Document Libraries

      SPSite siteCollection = new SPSite("Http://Mohan");
        SPWebCollection sites = siteCollection.AllWebs;
        foreach (SPWeb site in sites)
        {
            //SPListCollection:- object that represents the collection of lists in a site
            SPListCollection lists = site.Lists;
            foreach (SPList list in lists)
            {
                if (list.BaseType == SPBaseType.DocumentLibrary)
                {
                    //SPDocumentLibrary:- Represents a document library in Windows SharePoint Services. displays the name of the site and list, as well as the file name, for each item in every document library.
                    SPDocumentLibrary docLibrary = (SPDocumentLibrary)list;
                    if (!docLibrary.IsCatalog && list.BaseTemplate != SPListTemplateType.XMLForm)
                    {
                        //SPListItemCollection :- Represents a collection of SPListItem objects. To return the collection of items for a list or document library
                        SPListItemCollection docLibItems = docLibrary.Items;
                        foreach (SPListItem docLibItem in docLibItems)
                        {
                            Label1.Text += docLibItem.Name + "<br>";
                            //Label1.Text +=  " Docuement title -- " + docLibItem["Title"] + "<BR>";
                        }
                    }
                }
            }
        }

Adding Item to a task list

//Adding the items to task(Custome task list earliar created)list
        SPSite rootSite = new SPSite("Http://Mohan/site1");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPList taskList = web.Lists["Custom Task List"];
        //Adding new task
        SPListItem newTask = taskList.Items.Add();
        newTask["Title"] = "Work on SPS 2010/13";
        newTask["PercentComplete"] = 0.1;
        newTask.Update();
        Response.Write("items manupulation completed..");

Creating the Task List

        //Adding new list -- taking the task list as template and creating new list item
        // Create site1 and implement
        SPSite rootSite = new SPSite("Http://Mohan/Asite");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPListTemplate sourceTemplate = web.ListTemplates["Tasks"];
        Guid newListGuid = web.Lists.Add("Custom Task List", "Custom Task List", sourceTemplate);
        SPList newList = web.Lists[newListGuid];
        newList.Description = "Modified Custom Task List";
        newList.Update();
        Response.Write("Is added succes fully..");

Accessing List Item Values

//Accessing List Item Values
        //Create one site and a task with some items
        SPSite rootSite = new SPSite("Http://Mohan");
        SPWeb web = rootSite.AllWebs["Site1"];
        //SPList :- Represents a list on a SharePoint Web site.
        SPList taskList = web.Lists["task1"];
        //SPListItem :- Represents an item or row in a list. 
        foreach (SPListItem item in taskList.Items)
        {
            Response.Write("Item Name " + item.Name + "Assigner To " + item["AssignedTo"] + "Due Date " + item["DueDate"] + "<br>");
        }
        //Create one links item and add no items for test
        SPList lst = web.Lists["Links1"];
        foreach (SPListItem item1 in lst.Items)
        {
            Response.Write("Url Name " +  item1.Name + "<br>");
        }