locked
ClientContext not working RRS feed

  • Question

  • Im trying to add a webpart to the page on my office 365 site using the following code:

     
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {            
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    
    
                    using (SPWeb web = site.OpenWeb())
                    {
                        try
                        {
                            
                            ClientContext clientcontext = new ClientContext(site.Url);
                            
                            Microsoft.SharePoint.Client.File page = 
    clientcontext.Web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + 
    Lists.RiskRegisterURL + "/NewForm.aspx");
                            LimitedWebPartManager wpm = 
    page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
                            string webpartxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                                "<webParts>" +
                                                " <webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">" +
                                                "   <metaData>" +
                                                "      <type 
    name=\"ProductDeletion.HideProductDeletionFields.HideProductDeletionFields,
     ProductDeletion, Version=1.0.0.0, Culture=neutral, 
    PublicKeyToken=868bff825b111fd8\" />" +
                                                "      
    <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>"
     +
                                                "      <Solution 
    SolutionId=\"b21d5ce7-59f7-4a61-a91c-3f27225dbe57\" 
    xmlns=\"http://schemas.microsoft.com/sharepoint/\" />" +
                                                "    </metaData>" +
                                                "    <data>" +
                                                "      <properties>" +
                                                "        <property 
    name=\"Title\" type=\"string\">Product Deletion Hide 
    Fields</property>" +
                                                "        <property 
    name=\"Description\" type=\"string\">Hides fields on the modal popup 
    windows</property>" +
                                                "      </properties>" +
                                                "    </data>" +
                                                "  </webPart>" +
                                                "</webParts>";
                            WebPartDefinition wpd = wpm.ImportWebPart(webpartxml);
                            
                            wpm.AddWebPart(wpd.WebPart, "Left", 1);
                            clientcontext.ExecuteQuery();
                            
                        }
                        catch (Exception ex)
                        {
                            Log(site, ex.Message);
                        }
                    }
                }
                        
                
               
            } 


     

    However Im finding that the very first line is actually the one giving me the error:

                           
    ClientContext clientcontext = new ClientContext(site.Url);

    The url being passed in there is the full url 

    Of course being the online version, i cant actually tell what that error is, i just get the usual:

    "Sandboxed code execution request failed."

    Is there some sort of claims or other authentication i should be doing? and if so, why? The code is running as the site collection administrator within the context of SharePoint. So its not like there is an authentication issue?

    Monday, June 24, 2013 10:49 PM

All replies

  • Since it is a sanboxed solution & you will install the feature using the site collection admin so  code should have necessary rights.
    Wednesday, June 26, 2013 5:46 AM
  • >>public override void FeatureActivated(SPFeatureReceiverProperties properties)

    This is server side object model event;

    And the clientcontext is part of client object model.

    Wednesday, June 26, 2013 6:58 AM
  • opps i didn't notice that.
    Wednesday, June 26, 2013 7:53 AM
  • Ok so maybe im confused.

    The SPLimitedWebPartmanager is not supported in SharePoint online. So how can i add webparts to the site on feature activation?

    Wednesday, June 26, 2013 10:41 PM