Developer forum

Forum » Development » Programmatically updating indexes

Programmatically updating indexes

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

In the past I was able to update a remote index by posting to a URL like this:

http://sitename/Admin/Api/repositories/build/Products/Products-index-index/B/Full%20Catalogue

However, when I do that now, I get a 401 - Unauthorized on DW 9.4.18. 

I did some digging and see that RepositoryAuthenticationAttribute is now applied to the IndexController in 9.4.x. However, I am not sure what to do to pass the authentication requirements.

Any ideas?

Imar

 


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Imar,

I would recommend using a repository task / scheduled task instead - if possible.

If you use the web api then you need to pass the remote installation checksum (Settings > System information) in an authentication header.

Here is an example...

string responseString = null;
 
using (var client = new HttpClient())
{
    var checksum = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    var base64Checksum = Convert.ToBase64String(Encoding.Default.GetBytes(checksum));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Checksum);
 
    var requestUrl = "http://sitename/Admin/Api/repositories/build/Products/Products-index-index/B/Full%20Catalogue";
                
    using (var response = client.PostAsync(requestUrl, null).Result)
    {
        response.EnsureSuccessStatusCode();
        responseString = response.Content.ReadAsStringAsync().Result;
    }
}

/Morten

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hmmm, it seems I can send a base64 encoded version of the Installation.Checksum in the Authorization header when sending the HTTP request

var checksum = Dynamicweb.Content.Management.Installation.Checksum;
byte[] textAsBytes = Encoding.ASCII.GetBytes(checksum);
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(textAsBytes));

The checksum seems to consist of three parts:

- domain name
- machine name
- path to the Files folder

I can predict the first two of my connected remote cluster instances, but I am not sure about the Files folder. If someone relocates the site, my remote index builder will fail.

Is there an alternative to using the checksum to get the controller to work?

Thanks,

Imar

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks Morten, looks like you sent your message while I was typing mine.

>> I would recommend using a repository task / scheduled task instead - if possible.

Would love to but it's not always possible. We have a few setups with multiple frontend nodes and only one "Admin". Running the scheduled task would only run on the admin node. Even if we could get them to work on each machine, I still can't supply an on demand solution which is often needed (someone pushes a bunch of new products and then needs to update the index on all connected machines). That's why we built this Index Updater module which finds all connected Cluster Instances and makes a remote call.

Would be nice to have something built-in for this. We don't use a shared file system but a synced file system which I believe is part of the problem for us.

Thanks!

Imar

 

You must be logged in to post in the forum