Tuesday, 16 April 2013

Get all the users in the groups

// Get all the users in the groupsSPSite site = new SPSite("http://spsportal:8181");

SPWeb web = site.OpenWeb();web.AllowUnsafeUpdates =
true;
try{
foreach (SPGroup group in web.SiteGroups){
Response.Write(
"<b> " + group.Name + "</b> <br>");
foreach (SPUser user in group.Users){
Response.Write(
"-" + user.Name + "<br>");}
}
}

catch (Exception ex){
Response.Write(ex.Message);
}

Create a group with role permissions

//Create a group and Role permissionsSPSite site = new SPSite("http://Madan:8181");

SPWeb web = site.OpenWeb();web.AllowUnsafeUpdates =
true;
try{
SPMember siteOwner = web.SiteAdministrators[0];
//Assign the group name string grpName = "MVMMohanGroup";
//check if group exists bool grpExists = false;
foreach (SPGroup group in web.SiteGroups)
if (group.Name.ToLower() == grpName.ToLower())grpExists =
true;
if (!grpExists){
web.SiteGroups.Add(grpName, siteOwner,
null, "Eve Custom Group that I created because I have the permissions!");web.Update();

//get a reference to the group we just createdSPGroup customGroup = web.SiteGroups[grpName];
//add the group to the quicklaunchweb.Properties["vti_associategroups"] = web.Properties["vti_associategroups"] + ";" + customGroup.ID.ToString();
//Assign the roles for itSPGroup Role2Group = web.SiteGroups[grpName];
//Get the designer role definition to assign to our custom group any custom role alsoSPRoleDefinition mydesignerRoleDefinition = web.RoleDefinitions["View Only"];
//assign designer role to our custom group at the web levelSPRoleAssignment roleAssignment = new SPRoleAssignment(Role2Group);roleAssignment.RoleDefinitionBindings.Add(mydesignerRoleDefinition);
web.RoleAssignments.Add(roleAssignment);
customGroup.Update();
web.Properties.Update();
Response.Write(
"Group is created.");}

else{
Response.Write(
"Group is already created.");}
}

catch (Exception ex){
Response.Write(
"Group is not created." + ex.Message);}

Wednesday, 16 January 2013

http://localhost:32843/SecurityTokenServiceApplication/securitytoken.svc/actas

Error occurred in deployment step ‘Install App for SharePoint’: App Management Shared Service Proxy is not installed.

While deploying getting this error..
“App Management Shared Service Proxy is not installed”
To resolve this..
- Go to Central Admin > Application Management > Manage service applications.. make sure that the following services are running..
  • App Management Service
  • Secure Store Service
  • Security Token Service Application

Error occurred in deployment step ‘Install App for SharePoint’: Failed to install App for SharePoint.

Once you have the necessary services started, Follow the steps provided in the “Configure an isolated app domain to create and deploy SharePoint-hosted apps” section of this article.. How to: Set up an on-premises development environment for apps for SharePoint

For step#2: Set-SPAppDomain “your app domain” .. I used: Set-SPAppDomain “apps.sp2013domain.com”

So when I set the IE proxy “Add your isolated app domain to your bypass list in Internet Explorer”, here is the settings that worked for me



For step#7: if you get the error
“Set-SPAppSiteSubscriptionName : The requested service, ‘http://localhost:32843/SecurityTokenServiceApplication/securitytoken.svc/actas’ could not be activated. See the server’s diagnostic trace logs for more information.”

just do an iisreset or free up some RAM memory. This service needs at least 5% of the total RAM for its purpose.

BDC


public static Dictionary<Int32, Customer> d = null; public static Customer ReadItem(Int32 id) { // take a copy for SharePoint
Customer c = new Customer();
Customer e = d[id];
c.CustomerId = e.CustomerId;
c.FirstName = e.FirstName;
c.LastName = e.LastName;
c.Message = e.Message;
return c;
}
public static IEnumerable<Customer> ReadList()
{
// this is usually the first method called so check for null
if (d == null)
{
d = new Dictionary<Int32, Customer>();
for (int i = 0; i < 10; i++)
{
Customer e = new Customer();
e.CustomerId = i;
e.Message = i + " Item Data";
e.FirstName = i + " First Name";
e.LastName = i + " Last Name";
d.Add(i, e);
}
}
return d.Values;
}
public static void Update(Customer customer, Int32 id)
{
d[id].FirstName = customer.FirstName;
d[id].LastName = customer.LastName;
d[id].Message = customer.Message;
}

Tuesday, 1 January 2013

Windows Authentication Mode= error

<configuration>
 <system.web>
  <compilation debug="true">
   <assemblies>
    <add assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="Microsoft.SharePoint.Client, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
    <add assembly="Microsoft.SharePoint.Client.Runtime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/></assemblies></compilation></system.web>
 <system.webServer>
  <security>
   <authorization>
    <add accessType="Deny" users="?"/>
   </authorization>
  </security>
 </system.webServer>
</configuration>

Tuesday, 20 November 2012

Create a Workflow Using VS 2010 with an initiation Form and Process the Data in a SP list.

Create a Workflow Using VS 2010 with an initiation Form and Process the Data in a SP list.


1.       Create a Customer List in a SharePoint Site, with required columns.
2.       Create a new VS project File, New, New Project, Select C#, SharePoint, 2010, Sequential Workflow and Give the Project name as Mohan_WFSample1.
3.       Click on Ok,  Enter the Url Http://Madan:8181( Give your site URL). Select Deploy as a farm solution, Click on next, Enter the Workflow name (CustomerWF), Select List Workflow click on next.
4.       The library or list to associate your workflow with = select your list/library (ex:- Customer).
5.       The history list to display … = Workflow history
6.       The task list to display … = Tasks.
7.       Click on next.
8.       Select a User Manually starts the work flow?
9.       Now click on Finish.

*      Now create a new Workflow initiation Form to be used by the workflow.
v  In the solution explorer, with in project select Workflow1, right click on it , in the properties window click on Add, New item, Select Workflow Initiation Form, click Add.
*      Complete the Workflow initiation form
v  Add the following code to the main content placeholder
<br />
<br />
<br />
<asp:CheckBox ID="Checkbox" runat="server" Text="Fast Track Customer" />

v  Now open the WorkflowInitiationForm.aspx.cs
1.       In the GetInitiationData method replace returnstring.Empty; with the following code.

*      Now process Workflow initiation Form data in Workflow1

v  Open the view Designer of Workflow1.cs. From the Toolbox, add a Code activity, after the onWorkflowActivated1 activity.
v  Double click on codeActivity1 to generate an Execute Code event in code behind. Add the following code to the codeActivity1_ExecuteCode method. Set a Breakpoint in the method.
           If (workflowProperties.InitiationData == "True")
{
System.Diagnostics.Debug.WriteLine("This is a fast track customer");
}

*      Now Deploy the Workflow / Debug.
v  Press F5 to deploy and debug the new workflow
v  Navigate to the Customer list and create a new item in the list
v  Click on the edit dropdown for the item and select Workflows, Execute

Sampe API Codes

protected void Button1_Click(object sender, EventArgs e)
    {
        SPSite roosite = new SPSite(TextBox1.Text);
        Label1.Text = "Root collection found at Url " + roosite.Url.ToString() + " <br>  is part of Web Application " + roosite.WebApplication.Name;

    }

protected void Button2_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://madhanpc:8089");
        SPWebCollection sites = site.AllWebs;
        string[] names = sites.Names;
        foreach (string name in names)
            Response.Write(name + "<BR>");
    }

protected void Button3_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://madhanpc:8089");
        SPWeb web = site.AllWebs[0];
        foreach (SPList list in web.Lists)
        {
            Response.Write(list.Title + "  " + list.ItemCount + "<br>");
        }
        Response.Write(web.Lists.Count);
    }


protected void Button4_Click(object sender, EventArgs e)
    {
SPSite site = new SPSite("http://madhanpc:8089");
SPWeb web = site.OpenWeb();       
        web.AllowUnsafeUpdates = true;       
        SPWebCollection subsite = web.Webs;
        try
{
            SPWeb newSubWeb = subsite.Add("Morning123", "Morning", "Morning only", 1033, "STS#1", false, false);
            Response.Write("New site created.");
        }
catch (Exception ex)
        {
            Response.Write("Is not created.." + ex.Message);
        }
    }

protected void Button5_Click(object sender, EventArgs e)
    {
        //Accessing List Item Values
        //Create one site and a task with some items
        SPSite rootSite = new SPSite("Http://madhanpc:8089");
        SPWeb web = rootSite.AllWebs["MyTeam"];
        //SPList :- Represents a list on a SharePoint Web site.
        SPList taskList = web.Lists["tests"];
        //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>");
        }
    }

protected void Button6_Click(object sender, EventArgs e)
    {
        SPSite rootSite = new SPSite("Http://madhanpc:8089/myteam");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPList taskList = web.Lists["Tests"];
        //Adding new task
        SPListItem newTask = taskList.Items.Add();
        newTask["Title"] = "Create WP";
        newTask["PercentComplete"] = 0.1;
        newTask["Assigned To"] = "15;#MADHANPC\\emp1";
        newTask.Update();
        Response.Write("items manupulation completed..");

    }

protected void Button7_Click(object sender, EventArgs e)
    {
        SPSite rootSite = new SPSite("Http://madhanpc:8089/myteam");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPListTemplate sourceTemplate = web.ListTemplates["Tasts"];
        Guid newListGuid = web.Lists.Add("Task123", "Tasklist1232", sourceTemplate );
        SPList newList = web.Lists[newListGuid];
        newList.Description = "Modified Custom Task List";
        newList.Update();
        Response.Write("Is added succes fully.."); 
    }

protected void Button8_Click(object sender, EventArgs e)
    {
        //Get all document libraries from abcc site
        SPWeb myWeb = new SPSite("http://madhanpc:8089/myTeam/").OpenWeb();
        DropDownList1.DataSource = myWeb.Folders;
        DropDownList1.DataBind();
    }

protected void Button9_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/myteam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        try
        {
            // Add the document library.
            Guid newListID = web.Lists.Add("Doc","Test", SPListTemplateType.DocumentLibrary);
            // Set additional properties on the new document library.
            SPList newList = web.Lists[newListID];
            newList.OnQuickLaunch = true;
            newList.EnableVersioning = true;
            newList.Update();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

protected void Button10_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/MyTeam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        string path = "";
        // here filePath is html file control with runat="server" being used to pick the file browsed by user  
        string[] fileName = filePath.PostedFile.FileName.Split('\\');
        int length = fileName.Length;
        // get the name of file from path
        string file = fileName[length - 1];
        //  Get the library object where LibraryName is the name of Document Library.
        SPFolder folder = web.GetFolder("Doc");
        //
        SPFileCollection files = folder.Files;
        // Pick up the file in binary stream
        Stream fStream = filePath.PostedFile.InputStream;
        // Read file in a byte array
        byte[] MyData = new byte[fStream.Length];
        fStream.Read(MyData, 0, (int)fStream.Length);
        fStream.Close();
        SPFile fff = files.Add(file, MyData, true);
        web.AllowUnsafeUpdates = false;
    }

protected void Button11_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/MyTeam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        //Get the Document Library and its view
        SPList list = web.Lists["Doc"];
        SPListItemCollection items = list.Items;
        //Now we have to write a code for downloaing information of the document,
        //Read all documents and write those files to Local System
        foreach (SPListItem item in items)
        {
            //Write only names
            Response.Write(item.Name + "<br>");
        }
    }