Thursday 1 January 2015

Display Username of Current Logged In User using Javascript Client Object Model in Sharepoint


Reference

http://sharepoint.stackexchange.com/questions/73032/get-current-user-in-client-object-model-with-javascript


Add a webpart page to sharepoint site
Add content editor webpart
Edit content editor webpart
Copy and paste the content mentioned below in HTML mode and save the webpat page

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentUser;
function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
    document.getElementById('userId').innerHTML = currentUser.get_id();
    document.getElementById('userTitle').innerHTML = currentUser.get_title();
    document.getElementById('userEmail').innerHTML = currentUser.get_email();
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>
<div>Current Logged User:
    <span id="userLoginName"></span>
    <span id="userId"></span>
    <span id="userTitle"></span>
    <span id="userEmail"></span>
</div>
 
 

Display Title of a Sharepoint Site using Javascript Client Object Model


Thanks to Praveen..a simple program to display title of a sharepoint site using javascript client object model

http://praveenbattula.blogspot.in/2010/02/sharepoint-2010-client-object-model.html


Add a webpart page to sharepoint site
Add content editor webpart
Edit content editor webpart
Copy and paste the content mentioned below in HTML mode and save the webpat page


<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebSiteData, "sp.js");
var context = null;
var web = null;
    function getWebSiteData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
    }
    function onSuccessMethod(sender, args) {
        alert('web title:' + web.get_title() + '\n ID:' + web.get_id());
    }
    function onFaiureMethodl(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>



Add Items to List using ECMA/Javascript Client Object Model and Content Editor Webpart


Thanks a lot to sharepointtrac.wordpress.com explained step by step to add item to list in sharepoint using Javascript client object model.

Reference:

http://sharepointtrac.wordpress.com/2013/10/11/create-contact-form-using-ecmascriptjavascript-client-object-model-sharepoint-2010/

Create Contact form Using Ecmascript\Javascript Client Object model – SharePoint 2010

 

In continuation with my previous post about How to create Contact Forms in SharePoint 2010 here is the second option to create a Contact Us form using Content Editor Webpart with JavaScript and Ecmascript.
Resulting Form -

In the above Contact Us form when user Clicks on “Send” button a new item is created in “Contact Us” list using Ecmascript.
For Testing paste the below in content editor webpart and change the Contact Us list Name.

 

<script type=”text/ecmascript”>

function SendEnquiry()
{
var NameField = document.getElementById(“Name”);
var companyField = document.getElementById(“Company”);
var emailField = document.getElementById(“email”);
var enquiryField = document.getElementById(“Enquiry”);
SaveInContactUs(NameField.value,companyField.value,enquiryField.value);
}
function SaveInContactUs(Name,Company,Enquiry)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(‘Contact Us’);
var listItemCreationInfo = new SP.ListItemCreationInformation();
var newItem = list.addItem(listItemCreationInfo);
newItem.set_item(‘Title’, ‘Enq’);
newItem.set_item(‘Name’,Name);
newItem.set_item(‘Company’,Company);
newItem.set_item(‘Enquiry’,Enquiry);
newItem.update();
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
alert(‘Added!’); }
function failed(sender, args) {
alert(‘failed. Message:’ + args.get_message()); }
</script>
​​<table><tbody>
<tr><td>Name</td>
<td><input type=”text” id=”Name” name=”Name”/></td>
</tr>
<tr><td>Company</td>
<td><input type=”text” id=”Company” name=”Company”/></td>
</tr>
<tr><td>E-mail</td>
<td><input type=”text” id=”email” name=”E-mail”/></td>
</tr>
<tr><td>Enquiry</td>
<td><input type=”text” id=”Enquiry” name=”Enquiry”/></td>
</tr>
<tr><td><input type=”submit” onclick=”Javascript:SendEnquiry();” value=”Send”/></td></tr>
</tbody></table>

Once done you can fill-up the Form  and click Send and check the Contact Us list for details.
Make sure you have a Contact Us list created in your Site with the columns {Name, Company,email,Enquiry} used in this example.​​​​​​​​​​​​​​
Article from: http://www.learningsharepoint.com/2011/10/03/create-contact-form-using-ecmascriptjavascript-client-object-model-sharepoint-2010/

 




WISH YOU HAPPY NEW YEAR 2015


Friday 26 December 2014

Set Sunday of current week to Date and time column in a list using Formula


If we want to set Sunday of current week for a  column in a list then follow the steps below

Add a date and time column to a list

Name it as Date

Select Calculated value radio button under Default value section

in textbox enter formula =1-WEEKDAY(TODAY)+TODAY
 

[datetimeset.GIF]


Sunday of this current week:
 
=1-WEEKDAY(TODAY)+TODAY 

Monday of this current week:
 
=2-WEEKDAY(TODAY)+TODAY 

Tuesday of this current week:
 
=3-WEEKDAY(TODAY)+TODAY 

Wednesday of this current week:
 
=4-WEEKDAY(TODAY)+TODAY 

Thursday of this current week:
 
=5-WEEKDAY(TODAY)+TODAY 

Friday of this current week:
 
=6-WEEKDAY(TODAY)+TODAY 

Saturday of this current week:
 
=7-WEEKDAY(TODAY())+TODAY() 

SUNDAY of following week, when Monday & not Sunday is used as day #1 of week:
 
=8-WEEKDAY(TODAY())+TODAY()

Also, if you change the format of the cell to a custom format and choose DDD it will display Fri or Mon and if you use DDDD it will display Friday or Monday, etc.




Reference:

http://superuser.com/questions/376698/how-to-get-the-dates-of-the-current-monday-wednesday-and-friday-of-the-current

http://sridharu.blogspot.in/2009/01/set-default-date-time-to-datetime-field.html


Set column in a list to Read only using Sharepoint Designer



Using SharePoint Designer, you can make one of the columns on your list/library Read-Only so that when a user goes to edit the list item, that one particular field will be unmodifyable, even for Full Control Permissions.
This can be done by changing the field type from an “editable” field to a simple “display” field.
To do this, open up SPD and navigate to your list.
You want to create a new “Edit” form for this list so click on the “New…” button:
new-edit-form
Type in a name for your new form. Select the “Edit Item Form” radio button and go ahead and make this the default form for editing.
read-only-columns
Click OK and then open up your new form to edit the code.
In this particular library, I have a column that I named “Read-Only” which I will be changing to read-only mode in the edit form. To do this, scroll down to the column inside the code and change the following:
 
<SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="Edit" 
FieldName="Read_x002d_Only" __designer:bind="{ddwrt:DataBind('u',
concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(
string(@ID)),'@Read_x002d_Only')}"/>
<SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" 
FieldName="Read_x002d_Only" ControlMode="Edit"/>

to
 
<SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="Display"  
FieldName="Read_x002d_Only" __designer:bind="{ddwrt:DataBind('u', 
concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims( 
string(@ID)),'@Read_x002d_Only')}"/> 
<SharePoint:FieldDescription runat="server" id="ff3description{$Pos}"  
FieldName="Read_x002d_Only" ControlMode="Display"/>

Now when you go to edit an item on your form via “Edit Properties”, the column will be read-only.





Reference:

http://sharepoint.indigoreality.com/2013/04/03/sharepoint-2010-making-a-column-read-only/