0

I am trying to get the SourceID of the document in Async call using await keyword but I am not getting the value.

I am new to this concept, could anyone tell where I am doing mistake.

I have added my code in below code snippet:

public async GetListItems(ListTitle: string): Promise<any[]> {
   debugger;
   let ListCardItemsArray: ListCardListItem[] = [];
   let ListCardListItm: ListCardListItem = null;

   return new Promise<any[]>(async (resolve, reject) => {
       await sp.web.lists.getByTitle(ListTitle).items.select("*").get().then((results: any) => {
           results.map((result: any) => {
               let sourceID: string;
               ListCardListItm = new ListCardListItem();
               ListCardListItm["Title"] = result.Title;
               ListCardListItm["Description"] = result.Description;
               ListCardListItm["DocumentURL"] = result.DocumentURL;
              //here i am calling my method
               let sourceID =  this.GetSourceID("");
               ListCardListItm["SourceID"] = "";
               ListCardListItm["VisioURL"] = result.VisioURL;
               ListCardItemsArray.push(ListCardListItm);
           })
           console.log(ListCardItemsArray);
           resolve(ListCardItemsArray);
       }, (error: any) => {
           console.log("Error of GetListItems " + error);
           resolve(error);
       });
     });
}


public async GetSourceID(LinkURL: string) {
   let sourceID: string = "";
   try {
       let result = await sp.web.getFileByUrl("https://contosa.sharepoint.com/:u:/r/sites/Sample/SampleLibrary/Sample.vsdx?d=we836259ffe4b4f6c96ca6dd1118e6629&csf=1&web=1&e=B2w69d").select("*").get();
       sourceID = await result.UniqueId;
       console.log(sourceID);
       return sourceID;
   } catch (error) {
       console.log(error);
   }
}
0

Try this:

Change GetListItems() function to:

//here i am calling my method
let sourceID = await this.GetSourceID("https://contosa.sharepoint.com/:u:/r/sites/Sample/SampleLibrary/Sample.vsdx?d=we836259ffe4b4f6c96ca6dd1118e6629&csf=1&web=1&e=B2w69d");
ListCardListItm["SourceID"] = sourceID;

& use:

public async GetSourceID(LinkURL: string): Promise<string> {
    let sourceID: string = "";
    try {
      let result = await sp.web.getFileByUrl(LinkURL).select("*").get();
      sourceID = result.UniqueId;
      console.log(sourceID);
      return sourceID;
    } catch (error) {
      console.log(error);
      return sourceID;
    }
}
2
0

I tried the below code and it's working fine

public async GetSampleListItems(ListTitle: string): Promise<any[]> {
    debugger;
    let ListCardItemsArray: ListCardListItem[] = [];
    let ListCardListItm: ListCardListItem = null;
    let resultsArray: any = [];

    return await new Promise<any[]>(async (resolve, reject) => {
     try {
            resultsArray = await sp.web.lists.getByTitle(ListTitle).items.select("*").get();
            for (let index = 0; index < resultsArray.length; index++) {
                ListCardListItm = new ListCardListItem();
                const element = resultsArray[index];
                ListCardListItm["Title"] = element.Title;
                ListCardListItm["Description"] = element.Description;
                ListCardListItm["DocumentURL"] = element.DocumentURL;
                
                let sourceID = await this.GetSourceID(element.VisioURL ? 
                      element.VisioURL["Url"] : "");

                //sourceID = ;
                ListCardListItm["SourceID"] = sourceID;
                ListCardListItm["VisioURL"] = element.VisioURL;
                ListCardItemsArray.push(ListCardListItm);
            }
            //})
            console.log(ListCardItemsArray);
            resolve(ListCardItemsArray);
        } catch (error) {
            console.log("GetSampleListItems= " + error);
        }
    });
}

public async GetSourceID(LinkURL: string) {
        let sourceID: string = "";
        try {
            let result = await sp.web.getFileByUrl(LinkURL).select("*").get();
            sourceID = await result.UniqueId;
            console.log(sourceID);
            return sourceID;
        } catch (error) {
            console.log(error);
        }

    }

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.