SP Timer Job app to use REST Web Api to update data

Shri-6058 326 Reputation points
2020-12-03T21:00:44.843+00:00

Hello

I have an SP Timer job application with external web service but I want to use the internal SP list data now using REST API "public override void Execute(Guid targetInstanceId) { }"

I want to manage, populate, refresh sync one list data into another list properties via REST Web Api.
I believe I have to use HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");

but I don't see any any example or more details on how to generate a query REST to return based on parameter. For example, my SP list has all employee list of 5000. I want to return the record based on employee Number. etc.

I cant go with JQUERY approach as I have to build in C# sp timer job app. I believe HttpWebRequest is the option. Please point me some article or sample where I can execute a timer job?

Thanks
Shri

Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office SharePoint Server Development
{count} votes

4 answers

Sort by: Most helpful
  1. trevorseward 11,711 Reputation points
    2020-12-05T00:05:36.613+00:00

    It is strongly recommended that you do not create Timer Jobs as you need to take down the entire farm when you need to deploy or remove a timer job. I would suggest instead looking at executing this process from another server (i.e. Task Scheduler or some other mechanism).

    With that said, you'd use the server side object model to enumerate a list, i.e. create a new SPSite object to get SPWeb to get SPList.

    1 person found this answer helpful.
    0 comments No comments

  2. Baker Kong-MSFT 3,801 Reputation points
    2020-12-04T02:45:29.963+00:00

    Hi @Shri-6058 ,

    ...generate a query REST to return based on parameter. For example, my SP list has all employee list of 5000. I want to return the record based on employee Number. etc.

    We cannot pass parameters to timer job as it's a daemon app. You can store data somewhere( list, file etc), where your timer job would be able to access them.

    If you just want to filter items, you can append Odata $filter to the URL.

    As for C# sp timer job, you can take a reference of below samples, they often use SharePoint - Server Object Model to access list data.

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Shri-6058 326 Reputation points
    2020-12-04T07:20:56.353+00:00

    Thank you Baker Kong. I will check the references. I already have SP timer job in place. Its working just fine. I just want to change the source from web service to SP List itself. Let me explain what I am trying to achieve. Currently I have an external Content type SP list called "Employee_Report" which has all employee properties such as EmployeeCode, First Name, Last Name, Manager Name, Location, Department etc. This gets refreshed overnight to ensure all updated employee data(this is the same as web service data incoming into SP).
    I have another document library which contains employee folders(its one to one - one employee one folder). Each employee has one folder with his/her name where they store all the employee documents where only employee and his/her manager will have access. Now the SP timer job checks overnight if there are any new employee recorded in SP external list. If identified, it should create a folder for employee in document library and apply permission to self and manager. That's all.

    Currently I have a C# web service provides employee data with all properties(such as First Name, Last Name, designation, Manager Name, Location etc.) in the timer job method Execute(Guid targetInstanceId) { }. This is fully functional and working model for over year. All I want is cut off web service and use SP external list itself. May be using (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items"); No other functionality change in SP timer job. The reason I am trying httpWebRequest is because the Employee Report external list is in one web application and the document library is in anther web application otherwise I would have used just used simple object model.

    Please let me know if you have other suggestions.


  4. Shri-6058 326 Reputation points
    2020-12-04T17:46:37.837+00:00

    Thank you again. It is in the same farm. how do I access another web application? Should I hardcode the url? Also, will this scenario authentication prompts?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.