0

I'm trying to create a JavaScript script which will load when a user opens the SharePoint site. The code will fetch the ID of the current user and checks if there are any empty fields within the user Info List that are mandatory. If they are empty, the code will redirect the user to the edit profile screen to fill out their details.

Unfortunately, I've not created any scripts before and this will be my first time properly programming in JS, using the SP namespace, so I'm not sure on how to start this. Any help will be appreciated.

I'm using SharePoint 2013 Foundation for reference.

  • Thanks for the resources! I've spent more time looking for a solution to this and have got quite far with it. I've not yet finished, though I'll post the solution once I have. – Scott Brown Apr 18 '18 at 12:07
0

I would suggest doing some research and practice to understand what you are trying to do before putting code from someone else in your SharePoint without knowing what to do with it. There are great tutorials out there that will get you ready to do custom client development in SharePoint. Even "Hello World" in SharePoint is not exactly as easy as one would think. SharePoint is not straight forward when it comes to development. Here is some good places to get started:

0

I would also spend time learning about the User Profile and forget about the user info list. Any item in the user profile that is marked as replicable will populate the use info list. The User Profile is the proper source for this information and it feeds People Search. The User Info List is a wasteland of user information and a hold over from WSS. Every Site Collection has a different UIL.

You can also look at saving all the hassle and use Hyperfish to keep all of your profile information up to date. It prompts users for their information through Skype and Email.

0

Finished code below:

<script type ="text/javascript">

window.onload=function getCurrentUser() {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    currentUser = web.get_currentUser();
    clientContext.load(currentUser);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceeded(sender, args)
{
    currentUserId = currentUser.get_id();
    getCurrentUserDetails(currentUserId) 
}

function onQueryFailed(sender, args)
{
    alert(args.get_message());
}

function getCurrentUserDetails(currentUserId) {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var userInfoList = web.get_siteUserInfoList();
    itemUser = userInfoList.getItemById(currentUserId);
    clientContext.load(itemUser)
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded2),
        Function.createDelegate(this, this.onQueryFailed2)
    );
}

function onQuerySucceeded2(sender, args)
{
    name = itemUser.get_item("Title");
    site = itemUser.get_item("Site");
    department = itemUser.get_item("DepartmentNew");
    jobTitle = itemUser.get_item("JobTitle");
    manager = itemUser.get_item("Manager")
    email = itemUser.get_item("EMail")
    mobileNumber = itemUser.get_item("MobilePhone")

    isNameEmpty = false;
    isSiteEmpty = false;
    isDepartmentEmpty = false;
    isJobTitleEmpty = false;
    isManagerEmpty = false;
    isEmailEmpty = false;
    isMobilePhoneEmpty = false;

    if (name != "") {
        isNameEmpty = false;
    }
    else {
        isNameEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (site != "") {
        isSiteEmpty = false;
    }
    else {
        isSiteEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (department != "") {
        isDepartmentEmpty = false;
    }
    else {
        isDepartmentEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (jobTitle != "") {
        isJobTitleEmpty = false;
    }
    else {
        isJobTitleEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (manager != null) {
        isManagerEmpty = false;
    }
    else {
        isManagerEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (email != "") {
        isEmailEmpty = false;
    }
    else {
        isEmailEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
    if (mobileNumber != "") {
        isMobilePhoneEmpty = false;
    }
    else {
        isMobilePhoneEmpty = true;
        javascript:ShowPopupDialog('\u002f_layouts\u002f15\u002fuseredit.aspx?ID=' + _spPageContextInfo.userId);
    }
}

function onQueryFailed2(sender, args)
{
    alert(args.get_message());
}
</script>

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.