Not able to get webparts on a site page in Sharepoint - sharepoint

Using the below code using CSOM, I'm not able to fetch the webparts of a page in Sharepoint.
Microsoft.SharePoint.Client.File oFile = this._ClientContext.Web.GetFileByServerRelativeUrl(myurl );
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
this._ClientContext.Load(limitedWebPartManager.WebParts,
wps => wps.Include(
wp => wp.WebPart.Title,
wp => wp.Id,
wp => wp.WebPart.ExportMode,
wp => wp.WebPart.TitleUrl,
wp => wp.WebPart.Properties
));
this._ClientContext.Load(limitedWebPartManager.WebParts);
this._ClientContext.ExecuteQuery();
var areItemsavail = limitedWebPartManager.WebParts.AreItemsAvailable;
var webpartcount = limitedWebPartManager.WebParts.Count;
The areItemsavail above shows true, while the webpartcount shows the value 0.
There are lot of webparts available on the dedicated page.
Is there any way to fetch/get the webparts on a Sharepoint Site Page using CSOM ?

Which type of page you created?
webpart page:
publishing page:
not for modern page(site page)

Related

How to distinguish between personal site and OneDrive site types

I use the
public virtual SPOSitePropertiesEnumerable GetSitePropertiesFromSharePointByFilters(SPOSitePropertiesEnumerableFilter speFilter);
function to retrieve SharePointOnline sites:
do
{
sitesEnumerable = tenant.GetSitePropertiesFromSharePointByFilters(new SPOSitePropertiesEnumerableFilter
{
IncludePersonalSite = PersonalSiteFilter.Include ,
IncludeDetail = false,
StartIndex = nextIndex,
});
context.Load(sitesEnumerable, se => se.NextStartIndex,
se => se.NextStartIndexFromSharePoint,
se => se.Include(s => s.Url, s => s.Status, s => s.Template, s => s.Lcid,s => s.Title,s => s.HasHolds));
context.ExecuteQuery();
foreach (var site in sitesEnumerable)
{
//if site is OD site then
// ProcessAsODSite()
//else
// ProcessAsRegularSPOSite()
}
}while (nextIndex != null);
In this way OneDrive sites are retrieved together with the regular sites with no special order.
I want to process the OneDrive sites differently then i process the OneDrive sites differently then i am processing the SPO regural sites.
How can i distinguish between OneDrive sites and regular SPO sites for this matter?
Use the SitePrioerties.Template property to differentiate site types.
SPSPERS for one drive
STS for classic team sites
GROUP for modern team sites
SITEPAGEPUBLISHING for communication sites
etc
You can also prefilter to get just one kind of result with SPOSitePropertiesEnumerableFilter.Template

When trying to get all webparts on a Sharepoint Online page using C# CSOM, I get 0 returned (while there *are* webparts on my page)

So I'm using C# CSOM code to try and get all webparts, so that I can remove one. My Sharepoint Online page is just a standard modern teamsite page with nothing changed yet. I want to get all webparts, then remove the quick links standard webpart using csom. Here's my code:
Microsoft.SharePoint.Client.File oFile =
Context.Web.GetFileByServerRelativeUrl("/sites/CR-WST-GYM-20130306/SitePages/Home.aspx");
LimitedWebPartManager wpManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
/*Context.Load(wpManager.WebParts,
wps => wps.Include(
wp => wp.WebPart.Title));*/
Context.Load(wpManager);
Context.ExecuteQueryRetry();
WebPartDefinitionCollection wpDefinitionCollection = wpManager.WebParts;
Context.Load(wpDefinitionCollection);
Context.ExecuteQueryRetry();
It loads, but wpManager.WebParts contains 0 values & has a count of 0... how is this possible, when there's already standard webparts added to a newly created teamsite? Shouldn't I get at least a couple? What might I be doing wrong?
This code is taken from: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee539301(v%3Doffice.14) .
EDIT: I've also added a new webpart via UI to my main page to see if I get "1" as value, but it's still 0 ...
For "modern" site pages OfficeDevPnP.Core.Pages namespace has been introduced to manage web parts instead of Microsoft.SharePoint.Client.WebParts namespace
The following example demonstrates how to retrieve the list of client-side web parts on page
using (var ctx = new ClientContext(webUrl))
{
ctx.Credentials = GetCredentials(userName, password);
var page = OfficeDevPnP.Core.Pages.ClientSidePage.Load(ctx, "Home.aspx");
var webParts = page.Controls.Where(c => c.Type.Name == "ClientSideWebPart").ToList();
}
Prerequisites
SharePointPnPCoreOnline package
Reference
Customizing "modern" site pages
PnP isn't needed. This is working for me.
CamlQuery allPagesQuery = new CamlQuery();
ListItemCollection pageItems = list.GetItems(allPagesQuery);
ctx.Load(pageItems, pi => pi.Include(i => i.Id, i => i.DisplayName));
ctx.ExecuteQuery();
foreach (var item in pageItems)
{
ctx.Load(item, i => i.File, i => i.File.ServerRelativeUrl);
ctx.ExecuteQuery();
LimitedWebPartManager wpManager =
item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
ctx.Load(wpManager, wpm => wpm.WebParts.Include(wp => wp.WebPart.Title, wp => wp.Id));
ctx.ExecuteQuery();
foreach (WebPartDefinition wp in wpManager.WebParts)
{
log4.DebugFormat("Webpart in {0}: {1} [{2}]",
item.File.ServerRelativeUrl, wp.WebPart.Title, wp.Id);
}
}

SharePoint CSOM

Want to be build an application (using SharePoint CSOM) to fetch all the Users and SharePoint Groups from a SharePoint Farm. The Users and Groups are required to be fetched from all the Site Collections which may be present on the farm. Having gone thru the documentation it appears that the Site class https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.site_methods(v=office.14).aspx represents only a 'single' SiteCollection. This is great. However, before the application can create the Site objects for each SiteCollection the app need to determine all the SiteCollections present on the farm. Is there a class / method to retrieve all the SiteCollection on a SharPoint farm?
if the search is enabled, there is a way to do this by using search results:
KeywordQuery query = new KeywordQuery(site);
query.QueryText = string.Format("Path:{0} AND ContentClass:STS_Site", webAppURL);
query.RowLimit = 500;//max row limit is 500 for KeywordQuery
query.ResultsProvider = SearchProvider.Default;
query.EnableStemming = true;
query.TrimDuplicates = false;
query.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
query.KeywordInclusion = KeywordInclusion.AllKeywords;
SearchExecutor executor = new SearchExecutor();
ResultTableCollection resultTableCollection = executor.ExecuteQuery(query);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
Source: https://sharepoint.stackexchange.com/questions/133073/get-all-site-collections-with-csom
(By the way, with SharePoint Online it is easier, you can use SPOSitePropertiesEnumerable class.
SharePoint CSOM, retrieving site collections. Limited to 300?)

Filter Tags in Orchard CMS to only Blogs

In Orchard CMS, I'm trying to find a way to filter the Tags in the Tag Cloud to return tags only from blog posts. Right now, the Tag Cloud returns tags from all content types.
I'm using the default Tag Cloud in Orchard 1.7.1.
If there is a setting in admin, great, if not, I found a query in TagCloudService.cs. Is this the one I need to edit. If so, can someone help me with the filter I need to add here to include only blog posts
tagCounts = _contentManager
.Query<TagsPart, TagsPartRecord>(VersionOptions.Published)
.Join<CommonPartRecord>()
.Where(t => t.Container.Id == containerId)
.List()
.SelectMany(t => t.CurrentTags)
.GroupBy(t => t)
.Select(g => new TagCount {
TagName = g.Key,
Count = g.Count()
})
.ToList();
Thank you
Replace the call to Query with .Query().ForPart<TagsPart>().ForType("BlogPost").ForVersion(VersionOptions.Published).Join<TagsPartRecord>().

Is there some way in SharePoint to change document library views on a per-user basis?

We are using WSS 3.0 and I was asked to see if users can set default views on a per-user basis. Is anyone aware of any method (programatic or through the GUI itself) to give users the ability to change default views on a per-user basis? The 30 minutes of googling and poking around in the administrative menus turned out to be unfruitful. If not, is this a feature of MOSS 2007?
You probably want to look into audiences which is functionality in MOSS 2007.
Unfortunately it's not available in WSS 3.0
Here's a reasonable overview. User Profiles and Audience Targeting in SharePoint 2007
If you're working in WSS 3.0, you can programmatically swtich or modify views by using a webpart which gets the ListViewWebPart and modifies the query or view on the fly. Here is some sample code I am using to filter the contents of any given view:
private ListViewWebPart GetListViewWebPart()
{
ListViewWebPart webPart = new ListViewWebPart();
foreach (WebPart wp in WebPartManager.WebParts)
{
if (wp.GetType() == typeof(ListViewWebPart))
{
webPart = (ListViewWebPart)wp;
}
}
return webPart;
}
private void ApplyStrategySecurity(string camlFilter)
{
// Get the listview webpart
ListViewWebPart wp = GetListViewWebPart();
// Apply the query to the listview
XmlDocument doc = new XmlDocument();
doc.LoadXml(wp.ListViewXml);
if (camlFilter.Length > 0)
{
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode whereNode = queryNode.SelectSingleNode("Where");
if (whereNode != null)
queryNode.RemoveChild(whereNode);
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", string.Empty);
newNode.InnerXml = camlFilter;
queryNode.AppendChild(newNode);
}
wp.ListViewXml = doc.OuterXml;
}

Resources