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?)
Related
I need to add custom notifications to the personal Newsfeed on people's MySites. I found several tutorials and code examples for SharePoint 2010 on the net and tried to do the same with SharePoint 2013. They're all about creating ActivityEvents with the ActivityManager.
Here's the code I tried:
var targetSite = new SPSite("URL to MySite webapp");
SPServiceContext context = SPServiceContext.GetContext(targetSite);
var userProfileManager = new UserProfileManager(context);
var ownerProfile = userProfileManager.GetUserProfile("domain\\user1");
var publisherProfile = userProfileManager.GetUserProfile("domain\\user2");
var activityManager = new ActivityManager(ownerProfile, context);
Entity publisher = new MinimalPerson(publisherProfile).CreateEntity(activityManager);
Entity owner = new MinimalPerson(ownerProfile).CreateEntity(activityManager);
ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(activityManager, 17, owner, publisher);
activityEvent.Name = "StatusMessage";
activityEvent.ItemPrivacy = (int)Privacy.Public;
activityEvent.Owner = owner;
activityEvent.Publisher = publisher;
activityEvent.Value = "HELLOOOO";
activityEvent.Commit();
ActivityFeedGatherer.BatchWriteActivityEvents(new List<ActivityEvent> { activityEvent }, 0, 1);
The Id 17 in the CreateActivityEvent function is for the StatusMessage activity type, which is layouted like {Publisher} says: {Value} in the ressource files, so I provide the Value property of my ActivityEvent.
The code runs without any exception and in the User Profile Service Application_ProfileDB database I can see the right entries appear in the ActivityEventsConsolidated table.
But the activity is not visible in the activity feed, neither on the Owner's one, nor on the Publisher's one, even though these people follow each other. I ran the Activity Feed Job in the CA manually to update the activity feed.
Also, I tried to do the same with custom ActivityTypes with own ressource files, same result: The entry in the ActivityEventsConsolidated table (or ActivityEventsPublished if Owner=Publisher) appear, but no entries on the MySite.
Can anyone help?
I found the solution for this problem myself.
In Central Administration, Setup MySites, you have to enable the Enable SharePoint 2010 activity migration setting in the Newsfeed section in order to support SP1010 legacy activities in SP2013.
We have a requirement where users are to be allowed to search a list called "Reports" from the front-end, and advanced search, such as author:"John Smith" or filetype:docx, is to be supported.
We decided that this can be achieved using Sharepoint Server Search API (using the Keyword Query Language). Also, since it is possible that all users don't have access to all items in the list, we decided to go with the approach to use a web method with elevated privileges to query the list.
This is my attempt so far...
public string GetSearchResults(string listname, string searchQuery)
{
SPUser superUser = SPContext.Current.Web.AllUsers[#"SHAREPOINT\SYSTEM"];
using (SPSite site = new SPSite(SPContext.Current.Web.Url, superUser.UserToken))
{
KeywordQuery keywordQuery = new KeywordQuery(site);
keywordQuery.QueryText = searchQuery;
//where should listname be specified?
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
ResultTable resultTable = resultTables.FirstOrDefault();
DataTable dataTable = resultTable.Table;
}
//how to parse result to filter only items in given list?
}
...but what I don't understand is how to restrict search to only a given list or how to parse the search result to retrieve items of the list matching the search query.
I'm new to Sharepoint search, so any pointers would help.
This is possible either to give list path or list guid in query text
keywordQuery.QueryText = "Path:/Sales/Lists/Prospects/ (contentclass:STS_ListItem OR IsDocument:True) -ContentClass=urn:content-class:SPSPeople ";
I am a sharepoint newbee and am having trouble getting any search results to return using the search API in Sharepoint 2010 Foundation.
Here are the steps I have taken so far.
The Service Sharepoint Foundation Search v4 is running and logged in as Local Service
Under Team Site - Site Settings - Search and Offline Availability, Indexing Site Content is enabled.
Running the PowerShell script Get-SPSearchServiceInstance returns
TypeName : SharePoint Foundation Search
Description : Search index file on the search server
Id : 91e01ce1-016e-44e0-a938-035d37613b70
Server : SPServer Name=V-SP2010
Service : SPSearchService Name=SPSearch4
IndexLocation : C:\Program Files\Common Files\Microsoft Shared\Web Server Exten
sions\14\Data\Applications
ProxyType : Default
Status : Online
When I do a search using the search textbox on the team site I get a results as I would expect.
Now, when I try to duplicate the search results using the Search API I either receive an error or 0 results.
Here is some sample code:
using Microsoft.SharePoint.Search.Query;
using (var site = new SPSite(_sharepointUrl, token))
{
//
FullTextSqlQuery fullTextSqlQuery = new FullTextSqlQuery(site)
{
QueryText = String.Format("SELECT Title, SiteName, Path FROM Scope() WHERE \"scope\"='All Sites' AND CONTAINS('\"{0}\"')", searchPhrase),
//QueryText = String.Format("SELECT Title, SiteName, Path FROM Scope()", searchPhrase),
TrimDuplicates = true,
StartRow = 0,
RowLimit = 200,
ResultTypes = ResultType.RelevantResults
//IgnoreAllNoiseQuery = false
};
ResultTableCollection resultTableCollection = fullTextSqlQuery.Execute();
ResultTable result = resultTableCollection[ResultType.RelevantResults];
DataTable tbl = new DataTable();
tbl.Load(result, LoadOption.OverwriteChanges);
}
When the scope is set to All Sites I retrieve an error about the search scope not being available. Other search just return 0 results.
Any ideas about what I am doing wrong?
This is the workaround we came up with.
We did not get the foundation search to work as we had hoped. We will review it again once the RTM version of Sharepoint Foundation is released.
We installed Search Server Express 2010 beta. This allowed us to use the office server namespaces and the corresponding classes. This worked as expected and we were able to programmatically search against Sharepoint Foundation.
I also never got results by using FullTextSqlQuery on Foundation Search but perhaps you can use KeywordQuery:
SPSite thisSite = SPControl.GetContextSite(Context);
Microsoft.SharePoint.Search.Query.KeywordQuery kwQuery = new Microsoft.SharePoint.Search.Query.KeywordQuery(thisSite);
kwQuery.RowLimit = 1000;
kwQuery.QueryText = "searchString";
kwQuery.HiddenConstraints = "site:\"http://devXX:800/test/docs\"";
kwQuery.ResultTypes = ResultType.RelevantResults;
ResultTableCollection results = kwQuery.Execute();
ResultTable relevantResults = results[ResultType.RelevantResults];
dt.Load(relevantResults, LoadOption.OverwriteChanges);
I need to check for duplicates. Currently I have items stored in sub folders in a list.
How can I retrieve all items in the list from a web service, so I can check for duplicates?
Here is code from object model: I want to do exactly this, but from a web service
private static void PrintItemTitles()
{
string strUrl = "http://localhost:8099/";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyList"];
SPListItemCollection items = list.Items;
foreach (SPListItem item in items)
if (item != null)
Console.WriteLine(item.Title);
}
}
}
Use SPList.Items doesn't return all items? Well, then try SPList.GetItems(SPQuery).
Have a following SPQuery:
SPQuery query = new SPQuery();
query.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='Title'/>";
query.Query = String.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", someItemTitle)
query.MeetingInstanceId = -1; //In case if you query recurring meeting workspace - get items from all meetings
query.RowLimit = 10; //Will you have more than 10 duplicates? Increase this value
query.ViewAttributes = "Scope='RecursiveAll'"; //Also return items from folders subfolders
Note: There could be some mistakes in code, because i`m writing from top of my head
By executing this query and if it returns more than one item, then you have a duplicate!
Edit: Ahh, sorry, you are talking about Web Services.
Then this code won't help. There are 2 options then:
Option 1: You CAN create a view that
does include items even from folders
(flat view). See here for
instructions.
Option 2: According to Lists Web Service's GetListItems method, you can pass QueryOptions parameter. Pass in
<QueryOptions>
<MeetingInstanceID>-1</MeetingInstanceID> <!-- Again, if you query recurring meeting, you want ALL items -->
<ViewAttributes Scope='RecursiveAll' /> <!-- or Recursive if that does not work -->
</QueryOptions>
Good luck!
You can use the Lists.asmx web service, but that is quite hard to do as it returns quite a lot of information. I would deploy a custom web service on my SharePoint environment that contains that code and returns the list items.
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;
}