1

I really need your help on this.

I would like to know how i can retrieve current user's password and login from a ClaimClientContext.GetAuthenticatedContext.

What i want :

I'm trying to code a self-provisioning exe for a customer. My customer will have to log into his Sharepoint Online tenont, and then he will have to fill a window.Forms, choosing to which subsite he wants to add a subsite, then he will enter name, url and description.

What i have :

my customer's tenant is like this : the Root. Under the Root (let's call it X), 5 (or more) subsites (calling them Y). Under each subsite, 5(or more) other subsites (calling them Z).

The client wants to create Z subsites under the Y subsites. To do this, i created the following program :

static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string targetSite = ConfigurationManager.AppSettings["SpSiteURL"];
            try
            {
                using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
                {

                    Web web = ctx.Web;
                    ctx.Load(web); // Query for Web
                    ctx.ExecuteQuery(); // Execute 
//After authentication and ctx complete, run window form !

                    Application.Run(new createSiteParam(ctx));//run the window.forms
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
            }
        }

Now, in the window.forms, i have buttons, and i've their event handler, like the Click one. So when the customer clicks on the button "Create Subweb", it launches the following eventhandler :

private void btnCreate_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            /*Get all fields values*/
            string siloSelected = lstSilos.SelectedItem.ToString();
            string labName = txtLabName.Text;
            string labUrl = txtLabUrl.Text;
            string labDesc = txtLabDesc.Text;
            string labInitials = txtLabInitials.Text;
            string labCategory = txtLabCategory.Text;
            string labYammer = txtLabYamFeed.Text;
            bool doesWebExist = checkSubWebs(_ctx, siloSelected, txtLabName.Text);
            if (doesWebExist == true)
            {
                Console.WriteLine("subsite already exists");
            }
            else
            {
                Console.WriteLine("subsite doesn't even exist");
            }

            Console.WriteLine("Lab Name = {0}", labName);
        }

In my checkSubWebs bool, i check if the subsite already exists :

private bool checkSubWebs(ClientContext ctx, string parentName,string webTitle)
        {
            bool foundASiteTitle = false;
            try
            {
                if (ctx != null)
                {
                    Web web = ctx.Web;

                    ctx.Load(web, website => website.Webs, website => website.Title); // Query for Web
                    ctx.ExecuteQuery(); // Execute
                    foreach (Web orWebsite in web.Webs)
                    {
                        if (orWebsite.Title == webTitle)
                        {
                            foundASiteTitle = true;
                        }
                    }
                    return foundASiteTitle;
                }
                else
                {
                    return foundASiteTitle;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
                return false;
            }
    }

Here is for my code. I've got the claimAuth code from msdn : https://code.msdn.microsoft.com/office365/Remote-Authentication-in-b7b6f43c#content

What exactly is my problem :

Now, my problem is the following : when running the programm, i don't get any "subsite already exists". I found why : because in the checkSubWebs, i stay and parse only the Y level. Not the Z.

How could i go deeper in the Z?

I should have created a new ClientContext.

BUT, I don't know how to get the current user's password and login from the AuthClaim.

So please, could you give me more advices? Maybe there are other solutions to run it, like create a new ClientContext without using user's credentials ?

Thanks a lot !!

Have a nice day and a nice Easter.

0

Can you make the function recursive?

public bool SiteExists(ClientContext ctx, Web web, string webTitle)
{
    if(web.Title == webTitle) return true;
    ctx.Load(web, w => w.Webs.Include(w => w.Title));
    ctx.ExecuteQuery();
    foreach (var subWeb in web.Webs)
    {
        if(SiteExists(ctx, subWeb, webTitle)) return true;
    }
    return false;
}

Just call the method in your checkSubWebs function, passing in the root site, and it should recursively check all subsites.

I wrote this off the top of my head, so double check for errors.

Just realized I didn't really answer your question of how to copy the credentials. I dug into the GetAuthenticatedContext method in the link in your question. The authentication is being done by adding the federated cookies to every request. If you can modify this function to store and retrieve the cookies retrieved from ClaimClientContext.GetAuthenticatedCookies method, then you should be able to call the method and the user should only have to login the first time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.