none
SharePoint Client Object Model - Create a Web Part with Datasheet view/Calendar view as default view RRS feed

  • Question

  • Hi,

    I tried creating a web part on home page using sharepoint client object model with datasheet view as its default view however even if I set the view type as "GRID" correctly in my xml still after adding the web part we are getting the type as "HTML". We tried setting the XmlDefinition property as well however it did not work.

    Same thing works using Object Model as we are using reflection over there to set View Type property. 

    Please let us know how we can resolve this issue with client object model? 

    Note: We are facing same issue when we Calendar view as default as well.

           private void RetrieveWebDetails_CSOM(ClientContext context, ICredentials credentials)
            {
                Web web = context.Web;
                context.Load(web);
                context.Credentials = credentials;
                context.ExecuteQuery();
                string webTitle = web.Title;
                //MessageBox.Show(string.Format("Site '{0}' conected succesfully!", webTitle));
    
                List list = web.Lists.GetByTitle("Documents");
                context.Load(list);
                context.Load(list, l => l.Title, l => l.Views);
                context.ExecuteQuery();
                string listTitle = list.Title;
                //MessageBox.Show(string.Format("List with title '{0}' conected succesfully!", listTitle));
    
                var page = context.Web.GetFileByServerRelativeUrl("/sites/KejalSite/TestWEbpartSite/SitePages/Home.aspx");
                context.Load(page);
                context.ExecuteQuery();
                if (page.CheckOutType != CheckOutType.Online)
                {
                    // Check out  
                    page.CheckOut();
                }
                // Gets the webparts available on the page  
                var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
                context.Load(wpm.WebParts,
                    wps => wps.Include(wp => wp.WebPart.Title));
                context.ExecuteQuery();
                var availableWebparts = wpm.WebParts;
                var webPart = availableWebparts[0];
                var wpXML = "<webParts><webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\"><metaData><type name=\"Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" /><importErrorMessage>Cannot import this Web Part.</importErrorMessage></metaData><data><properties><property name=\"ShowWithSampleData\" type=\"bool\">False</property><property name=\"Default\" type=\"string\" /><property name=\"JSLink\" type=\"string\" null=\"true\" /><property name=\"CacheXslStorage\" type=\"bool\">True</property><property name=\"DisableViewSelectorMenu\" type=\"bool\">False</property><property name=\"XmlDefinitionLink\" type=\"string\" /><property name=\"ManualRefresh\" type=\"bool\">False</property><property name=\"ListUrl\" type=\"string\" null=\"true\"></property><property name=\"ListId\" type=\"System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\">2cf3cf59-ba00-4db9-a811-aa9955914799</property><property name=\"TitleUrl\" type=\"string\">/sites/KejalSite/TestWEbpartSite/Shared Documents</property><property name=\"EnableOriginalValue\" type=\"bool\">False</property><property name=\"Direction\" type=\"direction\">NotSet</property><property name=\"ServerRender\" type=\"bool\">False</property><property name=\"ViewFlags\" type=\"Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\">Html, TabularView, Hidden, Grid</property><property name=\"AllowConnect\" type=\"bool\">True</property><property name=\"ListName\" type=\"string\">{2CF3CF59-BA00-4DB9-A811-AA9955914799}</property><property name=\"HelpUrl\" type=\"string\"></property><property name=\"ViewContentTypeId\" type=\"string\" /><property name=\"ListDisplayName\" type=\"string\" null=\"true\" /><property name=\"AllowZoneChange\" type=\"bool\">True</property><property name=\"DisableSaveAsNewViewButton\" type=\"bool\">False</property><property name=\"ViewFlag\" type=\"string\" /><property name=\"DataSourceID\" type=\"string\" /><property name=\"ExportMode\" type=\"exportmode\">None</property><property name=\"AutoRefresh\" type=\"bool\">False</property><property name=\"AllowEdit\" type=\"bool\">True</property><property name=\"Description\" type=\"string\" /><property name=\"HelpMode\" type=\"helpmode\">Modeless</property><property name=\"BaseXsltHashKey\" type=\"string\" null=\"true\" /><property name=\"AllowMinimize\" type=\"bool\">True</property><property name=\"CacheXslTimeOut\" type=\"int\">86400</property><property name=\"ChromeType\" type=\"chrometype\">Default</property><property name=\"Xsl\" type=\"string\" null=\"true\" /><property name=\"ChromeState\" type=\"chromestate\">Normal</property><property name=\"CatalogIconImageUrl\" type=\"string\">/_layouts/15/images/itdl.png?rev=23</property><property name=\"SampleData\" type=\"string\" null=\"true\" /><property name=\"UseSQLDataSourcePaging\" type=\"bool\">True</property><property name=\"TitleIconImageUrl\" type=\"string\"></property><property name=\"PageSize\" type=\"int\">-1</property><property name=\"ShowTimelineIfAvailable\" type=\"bool\">True</property><property name=\"Width\" type=\"string\" /><property name=\"DataFields\" type=\"string\" /><property name=\"Hidden\" type=\"bool\">False</property><property name=\"Title\" type=\"string\">Documents</property><property name=\"PageType\" type=\"Microsoft.SharePoint.PAGETYPE, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\">PAGE_NORMALVIEW</property><property name=\"DataSourcesString\" type=\"string\" /><property name=\"AllowClose\" type=\"bool\">True</property><property name=\"InplaceSearchEnabled\" type=\"bool\">True</property><property name=\"WebId\" type=\"System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\">00000000-0000-0000-0000-000000000000</property><property name=\"Height\" type=\"string\" /><property name=\"GhostedXslLink\" type=\"string\">main.xsl</property><property name=\"DisplayName\" type=\"string\" /><property name=\"IsClientRender\" type=\"bool\">False</property><property name=\"XmlDefinition\" type=\"string\">&lt;View Name=\"{95F6EB48-4460-4B55-BCA7-46A7F40AF7DA}\" Type=\"GRID\" DisplayName=\"TDSViewCheck\" Url=\"/sites/KejalSite/TestWEbpartSite/Shared Documents/Forms/TDSViewCheck.aspx\" Level=\"1\" BaseViewID=\"1\" ContentTypeID=\"0x\" ImageUrl=\"/_layouts/15/images/dlicon.png?rev=44\" &gt;&lt;Query /&gt;&lt;ViewFields&gt;&lt;FieldRef Name=\"DocIcon\" /&gt;&lt;FieldRef Name=\"LinkFilename\" /&gt;&lt;FieldRef Name=\"Modified\" /&gt;&lt;FieldRef Name=\"Editor\" /&gt;&lt;FieldRef Name=\"ID\" /&gt;&lt;FieldRef Name=\"_UIVersionString\" /&gt;&lt;/ViewFields&gt;&lt;RowLimit Paged=\"TRUE\"&gt;30&lt;/RowLimit&gt;&lt;Aggregations Value=\"Off\" /&gt;&lt;JSLink&gt;clienttemplates.js&lt;/JSLink&gt;&lt;XslLink Default=\"TRUE\"&gt;main.xsl&lt;/XslLink&gt;&lt;Toolbar Type=\"Standard\"/&gt;&lt;/View&gt;</property><property name=\"InitialAsyncDataFetch\" type=\"bool\">False</property><property name=\"NoDefaultStyle\" type=\"string\" /><property name=\"AllowHide\" type=\"bool\">True</property><property name=\"ParameterBindings\" type=\"string\">&lt;ParameterBinding Name=\"dvt_sortdir\" Location=\"Postback;Connection\" /&gt;&lt;ParameterBinding Name=\"dvt_sortfield\" Location=\"Postback;Connection\" /&gt;&lt;ParameterBinding Name=\"dvt_startposition\" Location=\"Postback\" DefaultValue=\"\" /&gt;&lt;ParameterBinding Name=\"dvt_firstrow\" Location=\"Postback;Connection\" /&gt;&lt;ParameterBinding Name=\"OpenMenuKeyAccessible\" Location=\"Resource(wss,OpenMenuKeyAccessible)\" /&gt;&lt;ParameterBinding Name=\"open_menu\" Location=\"Resource(wss,open_menu)\" /&gt;&lt;ParameterBinding Name=\"select_deselect_all\" Location=\"Resource(wss,select_deselect_all)\" /&gt;&lt;ParameterBinding Name=\"idPresEnabled\" Location=\"Resource(wss,idPresEnabled)\" /&gt;&lt;ParameterBinding Name=\"NoAnnouncements\" Location=\"Resource(wss,noitemsinview_doclibrary)\" /&gt;&lt;ParameterBinding Name=\"NoAnnouncementsHowTo\" Location=\"Resource(wss,noitemsinview_doclibrary_howto2)\" /&gt;</property><property name=\"DataSourceMode\" type=\"Microsoft.SharePoint.WebControls.SPDataSourceMode, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\">List</property><property name=\"AutoRefreshInterval\" type=\"int\">60</property><property name=\"AsyncRefresh\" type=\"bool\">False</property><property name=\"FireInitialRow\" type=\"bool\">True</property><property name=\"MissingAssembly\" type=\"string\">Cannot import this Web Part.</property><property name=\"XslLink\" type=\"string\" null=\"true\" /><property name=\"SelectParameters\" type=\"string\" /></properties></data></webPart></webParts>";
    
                // Import the webpart xml  
                var importedWebPart = wpm.ImportWebPart(wpXML); //wpXML denotes the webpart xml file.  
                // Add it to page  
                var webPart = wpm.AddWebPart(importedWebPart.WebPart, "wpz", 0);
    
                context.ExecuteQuery();
    
                PropertyValues vals = webPart.WebPart.Properties;
                context.Load(vals);
                context.ExecuteQuery();
    
                webPart.WebPart.Properties["XmlDefinition"] = webPart.WebPart.Properties["XmlDefinition"].ToString().Replace("HTML", "GRID");
                webPart.SaveWebPartChanges();
                context.Load(webPart);
                context.ExecuteQuery();
    
                webPart.WebPart.Properties.RefreshLoad();
    
                PropertyValues vals1 = webPart.WebPart.Properties;
                context.Load(vals1);
                context.ExecuteQuery();
    
                page.CheckIn("Test", CheckinType.MinorCheckIn);
                context.ExecuteQuery();
            }


    Wednesday, April 5, 2017 4:56 AM

All replies

  • THis is possible in Server side only

    http://www.enjoysharepoint.com/Articles/Details/sharepoint-2013create-custom-list-template-set-default-view-as-20654.aspx


    Please remember to click Mark as Answer on the answer if it helps you

    Wednesday, April 5, 2017 5:41 AM
  • Hi,

    We are able to add datasheet view in list using client object model, however we are stuck in setting this view as default view for our home page webpart. Please guide us how we can resolve the same.

    Thanks

    Wednesday, April 5, 2017 8:59 AM