1

I'm a totally beginner and totally stuck to a point in my Sharepoint project.

What i would like to achieve:

  • part 1 : retrieve the current user's Department's name
  • part 2 : retrieve the department's website's url from a custom list using ctx, and jquery. (I'm totally new to REST, so please no REST answer)

Structure of the custom List:

  • name = "O Services"
  • field's name i would like to retrieve from that list : "URLSitesServices"
  • field's name of the department : "Service" So, for example, in O Services, i have an item called (field Name) "Information Service", whose URLSitesServices is "http://mysite/service".

So, on site homepage, if user's department is Information Service, i would like to go in O Services, search for the Service called "Information Service" and retrieve its URLSitesServices

What i've already done:

part 1 is complete, i'm able to retrieve the current user's department's name.

i'm stuck in part 2 : i don't know how to ask for O Services, i don't know how to retrieve the different items, nor their different fields...

Here is my code so far :

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
       SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {
           getMyProfileProperties(
              function(profileProperties){
                 var fullProperties = profileProperties.get_userProfileProperties();
                 var myDepartment = fullProperties["Department"];
                 if (myDepartment){
                    var ctx2 = new SP.ClientContext();
                    var targetList = ctx2.get_web().get_lists().getByTitle('Services Osiris');
                    console.log(myDepartment);
                 }else{
                    console.log("nothing");
                 }
              },
              function(sender,args){
                  console.log('Error occurred while getting user profile properties:' + args.get_message());
              }    
           );
       });
    });

My targetList does just return a array, no useful values. i don't know how to retrieve the items, i saw get_items() etc.. but it does not work (i'm sure i'm making a lot of mistakes too)

Coul you please give me some hints, some clues to begin with, please? Thanks a lot in advance!

1

You need to write a CAML Query to query the list. The query will try to get a result based on the department. Example:

var targetList = ctx2.get_web().get_lists().getByTitle('Services Osiris');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Service\'/>' + 
        '<Value Type=\'Text\'>' + myDepartment + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery); 
clientContext.load(collListItem, 'Include(URLSitesServices)');  
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));   
function onQuerySucceeded(sender, args) {    
    var listItemEnumerator = collListItem.getEnumerator();        
    while (listItemEnumerator.moveNext()) {
        alert(oListItem.get_item('URLSitesServices'));
        break;
    }    
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
} 
1

When I was(still am) new to the client object model I too stumbled a lot. But https://msdn.microsoft.com/en-us/library/jj163201.aspx this link helped me a lot. Very clear and easy to understand instructions. Take a look once.

I see that you've dived into it so you can directly skip to the code snippets.

I hope it helps.

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.