Get a subwiki XwikiContext

Hi,

What is the best way to get a XWikiContext of a subwiki?

The goal is to save a new user into a subwiki. The code is executed in the main wiki, thus not in the same context.

Thanks in advance,
Maarten

Depends where you are, in a Java component the standard way to access the current XWikiContext is using the provider:

@Inject
private Provider<XWikiContet> xcontextProvider;

private foo()
{
    XWikiContext xcontext = xcontextProvider.get();
}

Hi Thomas,

Thanks for your quick response.

What I mean is, how can I get a context from a subwiki, when I execute this function from the main wiki.
I want to get the context by passing the subwiki ID.
Or is this a wrong concept?

Thanks,
Maarten

The context is related to the thread and not the wiki. When you want to manipulate a subwiki you generally just switch the current wiki in the context. Depends what exactly you want to do since many APIs also take the wiki you want to manipulate and you don’t need to touch the context.

Hi Thomas,

I’m working on the oidc-authenticator. My goal is to store a new user in the subwiki, if the request is coming from a subwiki. When storing the user, the context has to be given. But I would like to manipulate that so that we can store the user (and groups etc) in the subwiki.

You don’t need to care much about the XWikiContext for this use case as long as you have a valid one. You just need to make sure you use the complete DocumentReference (so including the wiki identifier) when you get/save documents.

Thanks Thomas, this was the helpfull direction I needed! Now I’m able to store new users in my subwiki from the OIDC-Authenticator:

		WikiReference wikiRef = new WikiReference("mysubwikiname");
	DocumentReference docRef = xcontext.getWiki().getUserClass(xcontext).getDocumentReference();
	docRef.setWikiReference(wikiRef);

Note that DocumentReference is immutable so setWikiReference actually is a helper which returns a new reference.