Showing posts with label Client Object Model. Show all posts
Showing posts with label Client Object Model. Show all posts

Saturday, November 13, 2010

SP2010 Model window closing automatically

Scenario:
Now since SP2010 have lot of support in-build for client side programming, I thought to leverage the same for one of the Pop Up requirement. User will click a button and there will a model popup to select values , selected value will be returned back to main window.

Issue : Model window was poping up but getting closed right away.

Reason:
I was using a ASP.Net server side Button , and so I realized that it is calling the client script on button click which is poping up the model window properly but then as soon as postback happens it was getting closed.

Solution:
There are 2 solutions to it in my opinion
1. Switch to HTML input control
2. return false from the function which you are using to open model window, which will prevent the server side event.

Code:


function myCallback(dialogResult, returnValue) {alert('Hello');}

function openSPModel(
var options = {url: '/_layouts/mypage.aspx', width: 500, dialogReturnValueCallback:myCallback};

SP.UI.ModalDialog.showModalDialog(options);
return false;
}

Article:

Monday, August 23, 2010

Client Object Model Javascript and web url

Scenario:
While I found so many blog posts on how to use Client Object Model in javascript.

One thing was missing, they all had examples with hardcoded Urls. Now thats not gonna work for a real world, we need context info like web relative url.

Workaround:
I will call it a workaround until I found the perfect solution. Check the view source of any sharepoint page in SP2010. I found a javascript variable 'L_Menu_BaseUrl' which holds the web relative url.

Code:

function OpenDialog() {
var options = SP.UI.$create_DialogOptions();
options.url = L_Menu_BaseUrl + "/_layouts/xClient/MyPage.aspx";
options.width = 500;
options.height = 400;
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}
Solutions:
1. Using Object model itself read the ServerRelativeUrl into a variable and use it.

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(getWebProperties, "sp.js");

var relativeUrl;

function getWebProperties() {
var ctx = new SP.ClientContext.get_current();
this.web = ctx.get_web();
ctx.load(this.web, 'Title', 'Id', 'Created', 'ServerRelativeUrl');
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));
}

function onSuccess(sender, args) {
relativeUrl = this.web.get_serverRelativeUrl();
}

function onFail(sender, args) {
alert('failed to get list. Error:' + args.get_message());
}

</script>
2. Using code you can output a javascript variable with Web Server Url and use it.
 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "someData",  "<script type=\"text/javascript\">var WebRelativeUrl='" + SPContext.Current.Web.Url + "';</script>");
Articles:
SharePoint-2010-Client-Object-Model-for-JavaScript

Monday, August 9, 2010

Current Web Url in Silverlight

Scenario:
Client Object Model and Silverlight is best combo which I use often and always get this question on how to get the Current SharePoint Web Url from within Silverlight without passing it from initparam's manually.

Answer is simple

Steps:
1. Open App.xaml.cs and add the following using statements to the top of the file

using Microsoft.SharePoint.Client;
using System.Threading;
2. Add the following code to the beginning of the Application_Startup method.
ApplicationContext.Init(e.InitParams, SynchronizationContext.Current);
3. Open MainPage.xaml.cs and add the following using statement to the top of the file
using Microsoft.SharePoint.Client;
4. Use the following code to get the client context for current web
ClientContext context = new ClientContext(ApplicationContext.Current.Url);
context.Load(context.Web);

List listSharedDocuments = context.Web.Lists.GetByTitle("Shared Documents");
context.Load(listSharedDocuments);

Thursday, May 27, 2010

Error referencing SharePoint Client DLLs in Silverlight

Scenario:
I build up a new VM and was experimenting with Silverlight and SharePoint. I created a new Silverlight Application Project in Visual Studio. Then I tried adding the client object model dlls.

Microsoft.SharePoint.Client.Silverlight.dll
and
Microsoft.SharePoint.Client.Silverlight.Runtime.dll

located at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin" and surprisingly got this

error sl client

Sometime your project build just fine but you get referencing error at run time, something like this.

error sl client2

Solution:
Two step solution was
1. Copy both the binaries from
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\ClientBin" to "C:\SilverlightClientOM" folder.

2. Reference from the new folder