Showing posts with label Adding. Show all posts
Showing posts with label Adding. Show all posts

Wednesday, 18 April 2012

Ribbon customization : Adding functionality to Ribbon Button

SharePoint 2010 Ribbon customization : Add functionality to the button


pre-requisite
Ribbon Basics

Writing the script in scriptblock
In the basics you saw the command action of the button firing a javascript that was inline. Supposingly if you wanted to call a block of JS code .. you cannot write it inline. For that you will have to use a ScriptBlock attribute of the CustomAction node like the following..

Writing the script in an external js file
The same javascript function can be written in an external *.js file. To achieve this, right-click on the project and Add the layouts mapped folder. In the subfolder under the layouts folder, add a js file.

Next, modify the elements.xml as below

done.


Enabling EcmaScript(JavaScript) IntelliSense in VS2010
To add intellisense to SP COM, add the following lines at the top of the js file..



/// <reference name="MicrosoftAjax.js" />

/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />

/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />



After adding the above lines, you will notice that the intellisense for the sharepoint client object model is active.

Using Client Object Model to dispplay SharePoint Status bar
Lets modify the HelloWorld function to show the message in the status bar of SharePoint. Below is the modified javascript..

/// <reference name="MicrosoftAjax.js" />

/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />

/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />


function HelloWorld() {

    this.statusID = SP.UI.Status.addStatus("Hello World",

"A message from Mano <a href='#' onclick='javascript:closeStatus();return false;'>Close</a>.", true);

    SP.UI.Status.setStatusPriColor(this.statusID, "green");

}


function closeStatus() {

    SP.UI.Status.removeStatus(this.statusID);

}


Using Client Object Model to display current selected listItems and the list id
Now that we are able to access the SP namespace, we should be able to access the SharePoint entities like the site and the web for our use. In the code snippet we will be looking at how to get the selected items and the list Id from where these items are displayed.



function ShowListInformation() {

    // get all selected items

    var items = SP.ListOperation.Selection.getSelectedItems();

    var itemCount = CountDictionary(items);

    

    // get the current list

    var listID = SP.ListOperation.Selection.getSelectedList();


    // Remove all previous status

    SP.UI.Status.removeAllStatus(false);


    this.statusID = SP.UI.Status.addStatus("Selection Information", "Item Count : "

+ itemCount.toString() + " List ID : " + listID.toString(), true);

    SP.UI.Status.setStatusPriColor(this.statusID, "blue");

}





Note: Make sure that you change the CommandAction attribute in the elements.xml with the new function
Why is the above code important ?
This code opens a whole new arena for us to play with , including to enable the button based on a condition, which we will see as we dwell deeper.

Tip: Make sure that the latest code is used to render the button
1. Make sure the app pool is recycled
2. Make sure that the browser cache is cleared

Monday, 19 December 2011

Adding SPListItems in SharePoint 2007

OK, so for my first real post I'd like to do go through programatically adding an item to a SharePoint List. It's pretty simple, but there are some things that it took me quite some time to find out. Please excuse me if I'm going through easy stuff, its been a hectic three months finding out all this =). I'm using the Beta 2 with a MOSS  on a VPC.

Lets look at some C# code:
___________________________________________

  SPList myList = myWeb.Lists["My First List"];
  SPListItem myNewItem = myList.Items.Add();
  myNewItem["Title"] = "New Item";
myNewItem.Update();
___________________________________________

So in the first line I got a SharePoint list from my SPWeb object. Then I used the Add() method of the Items collection which creates and returns a new SPListItem. Then, since one usually wants to initalize a new item with some values, I've set the "Title" property of my item and then used Update() to push the changes to the database.


All nice and easy. Some problems here though: what we get is an item using the default content type and maybe we have several in one list. Lets' modify our code to change the content type while creating an item:
___________________________________________

*SPContentType myCType = myList.ContentTypes["Custom content type"];
SPList myList = myWeb.Lists["My First List"];
SPListItem myNewItem = myList.Items.Add();

*myNewItem["ContentTypeId"] = myCType.Id;
*myNewItem.Update();

  myNewItem["Title"] = "New Item";
*myNewItem["CustomSiteColumn"] = 3;
  myNewItem.Update();
___________________________________________

To change the item content type, I need my list content type Id. It's best to get this from the actual SPList object, than to have it hard-wired. myList.ContentTypes returns a collection of c.types from which I can get the one I need through an indexer. It's also best to check if the content-type exist with something like an if (myCType != null) block, but for this example I'll assume it's all set up. Next thing there is changing the "ContentTypeId" field of the new item. Then an update and yey! we can access the "CustomSiteColumn" field of our new item.

For those of you wracking your brains how I knew about ContentTypeId field it can be educational to run a foreach on all the fields of your SPListItem.Fields like the following:
___________________________________________
foreach (SPField f in myItem.Fields)
{
try
{
if (myItem[f.Title] != null)
{
Label1.Text +=
f.TypeAsString + " "
+ f.Title + " "
+ f.InternalName + " "
+ thisItem[f.InternalName] + "<br />";
}
}
catch (Exception ex)
{
Label1.Text = "Non-accessable field.<br />";
}
}
___________________________________________

This little snippet of code will print all the fields inside your SPListItem for you to examine. I found out about the "Path" property this way.
Anyhow back to our example. So we've programatically created a new list item.. but where? In the root folder most likely, depending on what Item collection we were opening I think. But lists also support folders and we'll often want to stick our item in a particular folder.. for instance for security reasons, or just to keep things nice and tidy. Lets try creating an item in a folder then:
___________________________________________

SPContentType myCType = myList.ContentTypes["Custom content type"];
SPList myList = myWeb.Lists["My First List"];
*
SPFolder myFolder = dm.myList.Folders[3];
*
SPListItem myNewItem = dm.myList.Items.Add(myFolder.ServerRelativeUrl, SPFileSystemObjectType.File, null);

  myNewItem["ContentTypeId"] = myCType.Id;
  myNewItem.Update();

  myNewItem["Title"] = "New Item";
myNewItem["CustomSiteColumn"] = 3;
  myNewItem.Update();
___________________________________________

Look at the second new line: there's the overloaded .Add() method of the SPListItemColection, which now accepts a Url, an object type and a leafName. The first is most important - the url of the folder where our new item is going to be placed. When programatically accessing SharePoint its best to grab the url from some object.. like the SPFolder object we want to stick our item in. Next thing we need is the SPFileSystemObjectType.. basically either File, Folder or Web. Now it gets complicated - behind each SPListItem there is an SPFile object with the same Guid. Same with a list folder. Each folder also has an Item object.

I'll try to go into that in more detail (how I understand it) later, for now it'll do to say you can add an SPFolder to the SPList the same way as in my example, except you use
SPFileSystemObjectType.Folder. And as for the leafName - that's autogenerated so just put null.

So that just about wraps it up. Don't forget to run myWeb.Dispose() on your SPWeb objects after you've finished with them..