Subscribe: SharePoint SharePoint SharePoint

Saturday, July 6, 2013

Hiding Quick Launch in SharePoint

Hiding the quick launch in SharePoint can be done in a number of different ways but the easiest way is by overriding the current css class implemented by "Quick Launch"  in Content Editor WebPart (CEWP) :


Paste the following code in a CEWP and it will hide the quick launch bar :
SP2010
#s4-leftpanel{
display:none
}
.s4-ca{
margin-left:0px
}

SP2013
.ms-core-navigation { DISPLAY: none }
#contentBox { margin-left: 0px }

Delete a list in sharepoint 2010 using powershell

Deleting a list through UI is sometimes a hectic job when your server is slow, so a better approach is to delete the list using powershell. This method is much faster and reliable :

$site = Get-SPSite <Site Collection Name>
$web= $site.openweb()
$lists = $web.Lists
$getList = $web.lists["ListName"]
$getList.Delete()

NOTE : Replace <Site Collection Name> with your current Site Collection Name.
            : You should have required access over ContentDB to delete the list using powershell.

How to get current user displayname with javascript in sharepoint ?

If you need to retrieve the name of the current logged in user in sharepoint , it can be easily done with the help of a ECMA script. Following code illustrates the same : 

<script type=”text/javascript”>
ExecuteOrDelayUntilScriptLoaded(getDisplayName,”sp.js”);
var currentUser;
function getDisplayName(){
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() {
alert( currentUser.get_loginName());
}

function onQueryFailed(sender, args) {
alert(‘Request failed. \nError: ‘ + args.get_message() + ‘\nStackTrace: ‘ + args.get_stackTrace());
}
< /script>

Friday, July 5, 2013

Content Editor Web Part script error

Many a times when we add a ECMA script to a CEWP, it does not work as expected. This mainly happens because CEWP does not start a server thread. ECMA script can only be executed when a server thread or worker process is associated to it. So in order to make it work, we need to add a function i.e. "ExecuteOrDelayUntilScriptLoaded". It first loads the "SP.js" file so that it can start a server thread.

SharePoint 2010


After loading the "SP.js" file , the ECMA script in the CEWP is executed without any error.

Following is the code depicting the same :

Saturday, June 22, 2013

Internal Names in site columns in SharePoint

While creating a custom list in SharePoint 2010 or 2013, we usually retain the "Title" column while create all the other columns manually. Now while programmatically using the column we specify "Tilte" as column name even if we have changed the name to something else.

e.g. if we have changed the "Tilte" column name to "EmpName" , then in ECMA script we will use :

listItem.set_item('Title', 'My New Item!');

"Title" is  a site column which has been predefined in the site. Similarly there are a large number of columns in site.

Each site column has two attributes i.e "Name" and "Display Name". "Name" refers to the internal name or the field while "Display Name"  is the name which is displayed in the UI. Now if the "Display Name"  is "Employee Name" then the "Name"  is mapped as "Employee_x0020_ Name"  because space is interpreted as '_x0020' .

Adding new ListItem to SharePoint List using ECMAScript

Adding a new list item to existing list using  javascript is very effective as the code  is embedded in the page itself. Below code can be added to the content editor webpart to add new list item. The code executes at Client Side, so its performance effective too.

<script type="text/javascript" src="//sharejpoint.googlecode.com/files/jPointLoader.js"></script>
<script type="text/javascript">

function createListItem() {
var clientContext = new SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("ContactsList");
var itemInfo = new SP.ListItemCreationInformation();
this.listItem = list.addItem(itemInfo);