Sharepoint Online Change WebPart View Query using C# / CSOM - c#

I have try to change default view query using C# but it's not working. I can change JSLink property, but not XmlDefinition. Any idea to workaround or what I'm doing wrong?
var webPart = listWebPart.WebPart.
clientContext.Load(webPart.Properties);
clientContext.ExecuteQuery();
webPart.Properties["XmlDefinition"] = newQuery;
listWebPart.SaveWebPartChanges();
clientContext.Load(listWebPart);
clientContext.ExecuteQuery();
It's list webpart on some page.

You can get the LimitedWebPartManager for the page with the GetLimitedWebPartManager function on SharePoint Online as well, MSDN. You can then load the WebParts using the LimitedWebPartManager as below:
var page = ctx.Web.GetFileByServerRelativeUrl(pageUrl);
LimitedWebPartManager wpMgr = page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
ctx.Load(wpMgr.WebParts);
ctx.ExecuteQuery();
This will load all the webparts on the page. You can then get the WebPartDefinition of the webpart you require using it's index:
WebPartDefinition webPartDef = wpMgr.WebParts[webpartIndex];
ctx.Load(webPartDef);
ctx.ExecuteQuery();
You can now finally update the desired property and save the definition:
webPartDef.WebPart.Properties["XmlDefinition"] = newQuery;
webPartDef.SaveWebPartChanges();
webPartDef.CloseWebPart();
ctx.ExecuteQuery();
Hopefully this will help you update your webpart properties

Related

How to find SharePoint documents by custom column value using Microsoft Graph API

In SharePoint Online site via my Office 365 account, I've added a column - "CustomerId" to my documents. I want to find all documents with CustomerId of 102 in C# (not in JavaScript).
So far, I'm able to get all files under a folder
var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
.Children.Request().GetAsync().Result;
Or see the same result in Graph Explorer
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/children
but I haven't figured out the correct syntax to get all documents (driveIems) using the custom column filter condition in C# or in Graph Explorer. Examples of things I've tried:
In C#
var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
.Search("fields/CustomerId eq 102").Request().GetAsync().Result;
In Graph Explorer
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/search(q='CustomerId eq 102')
Hope someone can help me out on this.
Update:
Previously I got the driveItemId from
var customerFolder = graphClient.Sites[siteId].Drive.Root
.ItemWithPath("CustomerGroup/IndustryGroup").Request().GetAsync().Result;
string driveItemId = customerFolder.Id;
I see I can get a ListItem
var customerFolder = graphClient.Sites[siteId].Drive.Root
.ItemWithPath("CustomerGroup/IndustryGroup").Request()
.Expand(d => d.ListItem).GetAsync().Result;
but I only found a list ID of "4" from customerFolder.ListItem.Id
How shall I get a list ID so that I can use it in graphClient.Sites[siteId].Lists[listId]?
I would suggest to utilize the following query:
https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?filter=fields/CustomerId eq 123&expand=driveItem
Explanation:
filter items in a list via filter query option
return associated drive items for a list item via expand query option
Here is an example for msgraph-sdk-dotnet:
var request = await graphClient.Sites[siteId].Lists[listId].Items.Request().Filter("fields/CustomerId eq 123").Expand(item => item.DriveItem).GetAsync();
foreach (var item in request)
{
Console.WriteLine(item.DriveItem.WebUrl);
}
Update
The underlying document library list (along with its properties) for a drive could be retrieved like this:
var list = await graphClient.Sites[siteId].Drive.List.Request().GetAsync();
Console.WriteLine(list.Id); //gives SharePoint List Id
Note: https://graph.microsoft.com/beta/sites/{site-id}/drive
endpoint returns the default drive (document library) for this site
Reference
Working with SharePoint sites in Microsoft Graph

Programatically add Append-Only Comments to Sharepoint List Item

I'm hoping to be able to append to the V3Comments (Append-Only Comments) column on a Sharepoint list item programmatically using Microsoft.SharePoint.Client.
I'm using a sequence similar to:
using (ClientContext context = new ClientContext("path_to_site"))
{
List list = context.Web.Lists.GetByTitle("list_name");
CamlQuery query = new CamlQuery();
ListItemCollection list_items = list.GetItems(query);
context.Load(list_items);
context.ExecuteQuery();
ListItem list_item = list_items[0];
list_item["V3Comments"] = "New comment.";
list_item.Update();
context.ExecuteQuery();
}
When I attempt to do this, the following InnerException is thrown:
Field or property "AttachmentFiles" does not exist.
Any pointers on what I need to get this working would be useful.
The list I am using had attachments enabled (despite not actually being used). After disabling the attachments, and leaving it for some time, it works as intended.
I wish I could explain the reason for this, but it would be pure speculation.
I've no idea how this would be done for a list with attachments.

Read text from Sharepoint WebPart [c#]

I want to read text form my webpart. Its a simple one: just a line of text - nothing else.
I can get properties of my webparts (like title, desc etc) but can't get the content of it. Any ideas how to retreive this information? Thanks in advance.
using (SPSite site = new SPSite("http://mysite/pwa/some_web"))
{
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile("default.aspx");
using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
foreach (Microsoft.SharePoint.Client.WebParts.WebPart wp in wpm.WebParts)
{
Console.WriteLine("Web part: {0}", wp.Title);
}
}
}
}
WebPart is the base class where other web part classes inherit from, and only contains the common properties such as Title. To read a specific property, you need to cast that wp object to the actual class of the web part.
Within the foreach loop:
MyWebPartClass mwp = (MyWebPartClass)wp;
Console.WriteLine(wp.SomeProperty);
Where MyWebPartClass inherits WebPart and declares the property SomeProperty. Note that you should also check first that the wp object is in fact a webpart of your class, in case there are more web parts on the same page

deleting sharepoint list item from listview?

I have get all items of a share-point list. Requirement is to delete list items form listview. Plz guide me how it can be done ?
I have idea that it can be done by using checkbox in listview.
I am not getting how to get checked items in listview and how to delete checked items from share point list ?
You can just use the SPListItemCollection.Delete method (assuming you're using the api). If not then use can use Lists.asmx to delete. For example ssomething like the code below will work
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists["List1"].Items;
SPListItem item = listItems[k];
listItems.Delete(k);
And if you are using SharePoint 2010 you can use client Object Model - here is howto for exactly your sceanrio: http://msdn.microsoft.com/en-us/library/ee539976.aspx
Johnv2020 answer shows 2 other ways of doing it:
if you can/need to use server side code use regular SahrePoint object model (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitemcollection.delete.aspx - for SharePoint 2010 version, page also contain link to same method in previous version - Windows SharePoint Service 3/MOSS 2007).
If you need to talk to older version of SharePoint from clinet (browser or client application) - use WebService - http://msdn.microsoft.com/en-us/library/cc824213(v=office.12).aspx

how to add a WebPart to all pages in a SharePoint site?

I am using SharePiont Server 2007 Enterprise with Windows Server 2008 Enterprise, and I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5. I want to know how to add a WebPart to all pages of a SharePoint Site? Any reference samples?
I want to use this WebPart to display some common information (but the information may change dynamically, and it is why I choose a WebPart) on all pages.
There are two ways to do this depending on your situation.
If the sites exist already, you need to iterate over the sites, adding the web part:
http://blogs.msdn.com/tconte/archive/2007/01/18/programmatically-adding-web-parts-to-a-page.aspx
If the sites do not exist then you can add the web part to the site template:
How to add a web part page to a site definition?
Here's the code from Shiraz's first link worked out a bit more:
(Note: This code is not optimized, for instance, looping through a List's Items collection is not something you should normally do, but seeing as this is probably a one time action there's no problem)
private void AddCustomWebPartToAllPages()
{
using(SPSite site = new SPSite("http://sharepoint"))
{
GetWebsRecursively(site.OpenWeb());
}
}
private void GetWebsRecursively(SPWeb web)
{
//loop through all pages in the SPWeb's Pages library
foreach(var item in web.Lists["Pages"].Items)
{
SPFile f = item.File;
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
//ADD YOUR WEBPART
YourCustomWebPart wp = new YourCustomWebPart();
wp.YourCustomWebPartProperty = propertyValue;
wpm.AddWebPart(wp, "ZONEID", 1);
f.Publish("Added Web Part");
f.Approve("Web Part addition approved");
}
// now do this recursively
foreach(var subWeb in web.Webs)
{
GetWebsRecursively(subWeb);
}
web.Dispose();
}

Categories

Resources