Office365 Share point - How to create query rule using CSOM C#.net
hi there,
i need create query rule in office 365. did in ‘search service application’ level - sharepoint (on premise), need implement same query rule concept in ‘site level’ underneath office 365. instead of ‘server side object model’ have using client side object model (csom - c#.net or powershell)
note: not sure implementation possibility of server side object model in office 365.
so far,
- i have taken legacy code of server side object model create query rule in sharepoint (on premise)
- i trying modify code (server side object model) client side object model, in-order implement query rules in office 365.
- while convert, modify ssom code level after struggling, not sure far have done required changes.
ssom csom changes summary
- used “clientcontext” class return context information such objects current web application, site, site collection (namespace:microsoft.sharepoint.client)
- made connection office 365 (sharepoint) using client context.
- tried “guid” “web.id” getting null reference error while loading web components itself.
- here after not clear changes required in csom
please me fix issue.
many in advance,
-bharathiraja
code:
server side object model code in internet
public static void serversideobjectmodel()
{
string siteurl = "https://dev.search.com";
string resultsourcename = "local sharepoint results";
using (spsite site = new spsite(siteurl))
{
using (spweb web = site.openweb("/en"))
{
spservicecontext context = spservicecontext.getcontext(spserviceapplicationproxygroup.default,
spsitesubscriptionidentifier.default);
searchserviceapplicationproxy searchappproxy = context.getdefaultproxy(typeof(searchserviceapplicationproxy))
as searchserviceapplicationproxy;
searchserviceapplicationinfo ssai = searchappproxy.getsearchserviceapplicationinfo();
if (searchappproxy != null)
{
guid searchappid = searchappproxy.getsearchserviceapplicationinfo().searchserviceapplicationid;
searchserviceapplication searchapp = searchservice.service.searchapplications.
getvalue<searchserviceapplication>(searchappid);
queryrulemanager queryrulemanager = new queryrulemanager(searchapp);
searchobjectowner searchowner = new searchobjectowner(searchobjectlevel.spweb, web);
searchobjectfilter searchfilter = new searchobjectfilter(searchowner);
federationmanager federmanager = new federationmanager(searchapp);
source resultsource = federmanager.getsourcebyname(resultsourcename, searchowner);
queryrulecollection qrcollection = queryrulemanager.getqueryrules(searchfilter);
createqueryrule(queryrulemanager, qrcollection, searchfilter);
}
}
}
}
client side object model
public static void clientsideobjectmodel()
{
using (clientcontext ctx = new clientcontext(sharepointlogindetails.spurl)) // url -sharepointlogindetails.spurl
{
securestring pwd = new securestring();
foreach (char c in sharepointlogindetails.password.tochararray()) // password
{
pwd.appendchar(c);
}
ctx.credentials = new sharepointonlinecredentials(sharepointlogindetails.username, pwd); // username
if (ctx != null)
{
web web = ctx.web;
ctx.load(web);
ctx.executequery();
guid searchappid = web.id;
console.writeline(web.id.tostring());
searchserviceapplication searchapp = searchservice.service.searchapplications.
getvalue<searchserviceapplication>(searchappid);
queryrulemanager queryrulemanager = new queryrulemanager(searchapp);
searchobjectowner searchowner = new searchobjectowner(searchobjectlevel.spsite);
searchobjectfilter searchfilter = new searchobjectfilter(searchowner);
federationmanager federmanager = new federationmanager(searchapp);
source resultsource = federmanager.getsourcebyname("local sharepoint results", searchowner);
queryrulecollection qrcollection = queryrulemanager.getqueryrules(searchfilter);
createqueryrule(queryrulemanager, qrcollection, searchfilter);
}
}
}
common method create qr
public static void createqueryrule(queryrulemanager queryrulemanager, queryrulecollection qrcollection,
searchobjectfilter searchfilter)
{
queryrule queryrule = qrcollection.createqueryrule("queryrulename", null, null, true);
// query matches keyword exactly
list<string> keywords = new list<string>();
keywords.add("test1");
keywords.add("test2");
keywordcondition kwcondition = queryrule.queryconditions.createkeywordcondition(keywords, true);
// promopted results
microsoft.office.server.search.query.rules.bestbetcollection bestbets = queryrulemanager.getbestbets(searchfilter);
queryaction queryaction = queryrule.createqueryaction(queryactiontype.assignbestbet);
microsoft.office.server.search.query.rules.bestbet bestbet = bestbets.createbestbet("resutl1", null, "description", true);
bestbet = bestbets.createbestbet("result2", new uri("http://mysearch.com"), "description", true);
regularexpressioncondition pncondition = queryrule.queryconditions.createregularexpressioncondition("pattern", true);
}
hi bharathiraja,
we have create query rule in office 365 manually in currently.
best regards,
dennis
please remember mark replies answers if help.
if have feedback technet subscriber support, contact tnmff@microsoft.com
SharePoint , Apps for Office and SharePoint > Developing Apps for SharePoint 2013
Comments
Post a Comment