Skip Ribbon Commands
Skip to main content

Vardhaman Deshpande | zevenseas | SharePoint Blog

:

Vardhaman Deshpande | zevenseas | SharePoint Blog > Posts > Add Web Part to Page using SharePoint Client Object Model:File not Found error.
May 15
Add Web Part to Page using SharePoint Client Object Model:File not Found error.

​When adding a web part to your page if you get the "file not found" error there is one small trick which you probably have to do.
You will most probably get this error if the page you are adding the web part to, is not in the root site of the site collection.
Here is the code to add a web part to your SharePoint page:

ClientContext clientcontext = new ClientContext("http://vardhaman/Sites/zevenseas/");
Microsoft.SharePoint.Client.File page = clientcontext.Web.GetFileByServerRelativeUrl("/Sites/zevenseas/SitePages/Home.aspx");
LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
string webpartxml = /*The webpart XML */;
WebPartDefinition wpd = wpm.ImportWebPart(webpartxml);
wpm.AddWebPart(wpd.WebPart, "Left", 1);
clientcontext.ExecuteQuery();

Notice that I have passed the entire url of the site when creating the ClientContext object. And also when I have to fetch the page to which the webpart should be added, I have passed the entire site relative url which includes the name of the site which I initially passed to the ClientContext Object.

I was stuck on this error for sometime because I tought that once I had passed the site URL ("http://vardhaman/Sites/zevenseas/") to the ClientContext object, I need to only pass the location of the page within the site ("/SitePages/Home.aspx") when fetching the page. But that did not turn out to be the case.

Have Fun Coding!

Comments

Thank you

Thanks:) It worked for me
 on 9/14/2012 1:24 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


ReCaptcha

 

Attachments

 

 Related Posts

 
Firing events inside a modal dialog in SharePoint 2010
Declaratively Setting Audience Targeting property for Publishing Page
Adding Custom FieldType with Custom Properties to List
Sharevolution : World's first AppStore for SharePoint
Creating publishing pages with powershell in SharePoint2010
I'm a Speaker at SharePoint Connections Amsterdam 2011
Released; Office365 Beta Deployment guide for Enterprises
SharePoint webservices overview
SharePoint Server 2010 performance and capacity test results and recommendations
Export List Data to Excel in SharePoint
Try catch blocks in JSOM for server error in SharePoint 2010
Office365 beta and the SharePoint 2010 Client Object Model
Get HTML from page using the SharePoint Client Object Model
Office 365 Sandboxed Solutions:Exceeded the daily resource usage quota.
Automatically let VS step through code : The [DebuggerStepThrough] attribute.
Bing Maps Silverlight Control in Office 365
JavaScript "L_Menu_BaseUrl" variable for SharePoint
SPSiteDataQuery in Sandboxed solution with multivalued metadata column
Feature activation with Client object model (ECMA script) and sandboxed solution
Overcoming the sandbox limitation…Client object model (ECMA script) volunteers adding web parts on page
Sorting Filtering client side .. Rich user experience with JQUERY and ECMA script client object model
Restricting web part adding on page for specific zones with wonderful SharePoint JavaScripting
ECMA script client object model and SQM collection in SP2010
Asset Picker with dynamically created HTML with JQuery in Sandbox solution
Conflicts of a C# developer and PowerShell
Check if page is in Edit Mode on Client Side in SharePoint 2010
Getting Site usage info in SP2010 using Client Object model
Configuring Failover Database in SharePoint 2010
SharePoint 2010 Image library with size options
Missing Site Pages in Personal Site
SharePoint 2010 Search capabilities
 

 Statistics

 
Views: 1569
Comments: 1
Tags:SharePoint 2010, SharePoint, Client Object Model, C#
Published:738 Days Ago