Instagram Searching for users by their username
Instagram Platform API deprecation
As of April 4th, Instagram has suddenly decided to close down most of their Platform API. This unfortunately also means that many of the features in this package are no longer working. You can read more about the changes to the Instagram Platform API via the links below:

Platform Changelog

API and Other Platform Product Changes

Searching for users by their username

The Instagram API doesn't provide a way to lookup a user by it's username, so you must first may a search for the username, and then get the ID from the search result that matches the username. In Skybrud.Social, you can do this through the Search method.

The method has a number of overloads, where the most simple overload just takes the search query as a parameter (aka the username). Other overloads lets you customize the search a bit more.

At the time of writing, the method will return up to the 50 first users matching your search query. Unfortunately the Instagram API doesn't support pagination, so you won't be able to fetch more than the 50 first users.

Anyways, here is some example code:

@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    // Make the request to the Instagram API
    InstagramSearchUsersResponse response = Model.Users.Search("bacon");

    // Iterate through the users from the response body
    foreach (InstagramUserSummary user in response.Body.Data) {

        <p>
            <strong>@(user.FullName ?? user.Username)</strong>
            <span>(@user.Id)</span>
        </p>

    }

}

If you for some reason want to limit the amount of users returned to - lets say 20 - you could use one of the overloaded methods - either by specifying the limit/count as the second parameter:

@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    // Make the request to the Instagram API
    InstagramSearchUsersResponse response = Model.Users.Search("bacon", 20);

    // Iterate through the users from the response body
    foreach (InstagramUserSummary user in response.Body.Data) {

        <p>
            <strong>@(user.FullName ?? user.Username)</strong>
            <span>(@user.Id)</span>
        </p>

    }

}

Or by specifying an instance of InstagramGetUserSearchOptions (this is actually what Skybrud.Social uses under the hood):

@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Options.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    // Make the request to the Instagram API
    InstagramSearchUsersResponse response = Model.Users.Search(new InstagramGetUserSearchOptions {
        Query = "bacon",
        Count = 20
    });

    // Iterate through the users from the response body
    foreach (InstagramUserSummary user in response.Body.Data) {

        <p>
            <strong>@(user.FullName ?? user.Username)</strong>
            <span>(@user.Id)</span>
        </p>

    }

}

The class currently only lets you specify the Query and Count properties, so not much extra here than the first two overloads.

Search for a specific user

In the scenario described earlier, if you just want to search for a specific user - eg. to find the ID of a user based on a username, you can make a search like this:

@using Skybrud.Social.Instagram.Models.Users
@using Skybrud.Social.Instagram.Responses.Users
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    const string username = "abjerner";

    // Make the request to the Instagram API
    InstagramSearchUsersResponse response = Model.Users.Search(username);

    // Find the first user with the specified username
    InstagramUserSummary user = response.Body.Data.FirstOrDefault(x => x.Username == username);

}

From what I can tell, Instagram will always return usernames as lowercase, so you should specify username parameter as lowercase as well (or make a check that isn't case-sensitive).