Monday, March 25, 2013

Can not connect to Windows Azure Cloud Service Web Role (Remote Desktop)

I have tried few days to connect to azure web role. I enabled the settings which is prompting when you going to publish the web role. But it didn't work for me. (The reason i found is i gave the name administrator). You can following alternative way to connect to your  web role.

How to connect to Windows Azure Web Role (Using Remote Desktop)

  • Create a Azure Web Role and publish it to azure.
  • When you are publishing enable the Remote Desktop for all roles.
  • If you use usernames such as admin, administrator, etc. it will not work. You can check it from
  • Go to Cloud services and Configure Menu

image

Then click the remote icon in the bottom

image

Then it will pop up a dialog which you can use and check for your remote configurations.

image

Monday, March 18, 2013

Break Permission inheritance and add custom permission to SharePoint 2013 List using JSOM (JavaScript Client Object Model)

First you need to refer relevant JavaScript files.

<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/ecmascript" src="/_layouts/SP.core.debug.js" />
<script type="text/ecmascript" src="/_layouts/SP.runtime.debug.js" />
<script type="text/ecmascript" src="/_layouts/SP.debug.js" />


Then approach is



  • Get reference to current clientcontext
  • get web site reference
  • get reference to existing list
  • break the inheritance
  • define a role
  • get the user
  • finally add user and related role to the list

<script type="text/ecmascript">
function CustomPermision() {
var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();

// provide the list name
oList = clientContext.get_web().get_lists().getByTitle('ListName');
oList.breakRoleInheritance(false, true);

//something@sm.onmicrosoft.com
var userobj = oWebsite.ensureUser("loginname");
var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);
role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor));
oList.get_roleAssignments().add(userobj, role)


clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded() {
alert("Updated");
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>


How to Add EveryOne (All Authenticated users) to permission group


in here you need to get the user which represent all users. Therefore you can use following code to archive that.


// All users , EveryOne , All Athenticated Users
var userobj = oWebsite.ensureUser("c:0(.s|true");
var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);
role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor));
oList.get_roleAssignments().add(userobj, role)


these are the role definitions you can use.


SP.RoleType.Guest
SP.RoleType.Reader
SP.RoleType.Contributor
SP.RoleType.WebDesigner
SP.RoleType.Administrator
SP.RoleType.Editor

Reference : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sproletype.aspx

Wednesday, March 13, 2013

Add Fields to SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

Other Related Posts

In My previous post i described how to add a list to SharePoint. In here I'm going to  describe how to add fields to existing list.  First you need to refer same JavaScript files in your script page (SP.core.js,SP.runtime.js,SP.js

There are few approaches that you can use to add fields to existing list. But in my personal opinion i like following approach because

  • It is easy rather that use XML to add entire field
  • Intelligence are support based on field type (text, choice, user ..)
  • Code is more readable than xml
<script type="text/ecmascript">
function addFields() {

var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();
//Geting reference to the list
oList = clientContext.get_web().get_lists().getByTitle('CustomList');

// Get filed collection
var fldCollection = oList.get_fields();

var f1 = clientContext.castTo(
fldCollection.addFieldAsXml('<Field Type="Text" DisplayName="NewField" Name="NewField" />', true, SP.AddFieldOptions.addToDefaultContentType),
SP.FieldText);
f1.set_title("CurrentDate");
f1.set_description("sample desc");
f1.update();


var f2 = clientContext.castTo(
oList.get_fields().addFieldAsXml('<Field Type="Choice" DisplayName="choice" Name="fldchoice" />', true, SP.AddFieldOptions.addToDefaultContentType),
SP.FieldChoice);
var choices = Array("Approved", "Rejected", "Progress");
f2.set_choices(choices);
f2.update();

clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}

function onQuerySucceeded() {
alert("List Field Updated");
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>


In this example I'm adding SharePoint Choice Field (SP.FieldChoice)  and SharePoint Text Field to existing SharePoint list.


Note: You can you addFieldAsXml and can give complete xml as a string to add the field as well.

Monday, March 11, 2013

Create a SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

JSOM (JavaScript client object model) heavily used in SharePoint App model. I'm going to post series of basic operations that can archive using jsom.

First add these references to your page.

//Add jquery version correctly   
<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/init.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>




Then add following script to create the list.


<script type="text/ecmascript">

function createList() {
var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();

var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('CustomList'); // list name
listCreationInfo.set_description('description'); // list description
listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type

oWebsite.get_lists().add(listCreationInfo);

clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),// when success
Function.createDelegate(this, this.onQueryFailed) // when failed
);
}
function onQuerySucceeded() {
alert("List Created");
}

function onQueryFailed(sender, args) {
alert("List Failed");
}

</script>


When creating a list you can use following templates


SP.ListTemplateType.GenericList
SP.ListTemplateType.DocumentLibrary
SP.ListTemplateType.Survey
SP.ListTemplateType.Announcements
SP.ListTemplateType.Contacts
SP.ListTemplateType.Events
SP.ListTemplateType.Tasks
SP.ListTemplateType.DiscussionBoard
SP.ListTemplateType.PictureLibrary
SP.ListTemplateType.DataSources
SP.ListTemplateType.XmlForm
SP.ListTemplateType.NoCodeWorkflows
SP.ListTemplateType.WorkflowProcess
SP.ListTemplateType.WebPageLibrary
SP.ListTemplateType.CustomGrid
SP.ListTemplateType.WorkflowHistory
SP.ListTemplateType.GanttTasks
SP.ListTemplateType.IssuesTracking