1+ // script.js
2+
3+ const siteUrl = "https://your-sharepoint-site-url" ;
4+ const listName = "YourListName" ;
5+
6+ // Fetch books from SharePoint
7+ async function fetchBooks ( ) {
8+ const response = await fetch ( `${ siteUrl } /_api/web/lists/getbytitle('${ listName } ')/items` , {
9+ headers : {
10+ "Accept" : "application/json;odata=verbose"
11+ }
12+ } ) ;
13+ const data = await response . json ( ) ;
14+ return data . d . results ;
15+ }
16+
17+ // Render books
18+ async function renderBooks ( ) {
19+ const books = await fetchBooks ( ) ;
20+ const bookList = document . getElementById ( "books" ) ;
21+ bookList . innerHTML = "" ;
22+ books . forEach ( book => {
23+ const li = document . createElement ( "li" ) ;
24+ li . innerHTML = `
25+ <strong>${ book . Book_Title } </strong> by ${ book . Author } <br>
26+ Status: ${ book . Status } <br>
27+ User: ${ book . User || "N/A" } <br>
28+ <button onclick="checkOutBook(${ book . ID } )">Check Out</button>
29+ <button onclick="checkInBook(${ book . ID } )">Check In</button>
30+ ` ;
31+ bookList . appendChild ( li ) ;
32+ } ) ;
33+ }
34+
35+ // Check out a book
36+ async function checkOutBook ( bookId ) {
37+ const userName = prompt ( "Enter your name:" ) ;
38+ if ( ! userName ) return alert ( "Name is required!" ) ;
39+
40+ await fetch ( `${ siteUrl } /_api/web/lists/getbytitle('${ listName } ')/items(${ bookId } )` , {
41+ method : "POST" ,
42+ headers : {
43+ "Accept" : "application/json;odata=verbose" ,
44+ "Content-Type" : "application/json;odata=verbose" ,
45+ "X-RequestDigest" : document . getElementById ( "__REQUESTDIGEST" ) . value
46+ } ,
47+ body : JSON . stringify ( {
48+ Status : "Checked Out" ,
49+ User : userName
50+ } )
51+ } ) ;
52+
53+ alert ( "Book checked out successfully!" ) ;
54+ renderBooks ( ) ;
55+ }
56+
57+ // Check in a book
58+ async function checkInBook ( bookId ) {
59+ await fetch ( `${ siteUrl } /_api/web/lists/getbytitle('${ listName } ')/items(${ bookId } )` , {
60+ method : "POST" ,
61+ headers : {
62+ "Accept" : "application/json;odata=verbose" ,
63+ "Content-Type" : "application/json;odata=verbose" ,
64+ "X-RequestDigest" : document . getElementById ( "__REQUESTDIGEST" ) . value
65+ } ,
66+ body : JSON . stringify ( {
67+ Status : "Available" ,
68+ User : ""
69+ } )
70+ } ) ;
71+
72+ alert ( "Book checked in successfully!" ) ;
73+ renderBooks ( ) ;
74+ }
75+
76+ // Search books
77+ document . getElementById ( "search-button" ) . addEventListener ( "click" , async ( ) => {
78+ const title = document . getElementById ( "search-title" ) . value . toLowerCase ( ) ;
79+ const books = await fetchBooks ( ) ;
80+ const filteredBooks = books . filter ( book => book . Book_Title . toLowerCase ( ) . includes ( title ) ) ;
81+ renderFilteredBooks ( filteredBooks ) ;
82+ } ) ;
83+
84+ // Filter books
85+ document . getElementById ( "filter-button" ) . addEventListener ( "click" , async ( ) => {
86+ const status = document . getElementById ( "filter-status" ) . value ;
87+ const user = document . getElementById ( "filter-user" ) . value . toLowerCase ( ) ;
88+ const books = await fetchBooks ( ) ;
89+ const filteredBooks = books . filter ( book => {
90+ return ( ! status || book . Status === status ) && ( ! user || book . User . toLowerCase ( ) . includes ( user ) ) ;
91+ } ) ;
92+ renderFilteredBooks ( filteredBooks ) ;
93+ } ) ;
94+
95+ // Render filtered books
96+ function renderFilteredBooks ( books ) {
97+ const bookList = document . getElementById ( "books" ) ;
98+ bookList . innerHTML = "" ;
99+ books . forEach ( book => {
100+ const li = document . createElement ( "li" ) ;
101+ li . innerHTML = `
102+ <strong>${ book . Book_Title } </strong> by ${ book . Author } <br>
103+ Status: ${ book . Status } <br>
104+ User: ${ book . User || "N/A" } <br>
105+ <button onclick="checkOutBook(${ book . ID } )">Check Out</button>
106+ <button onclick="checkInBook(${ book . ID } )">Check In</button>
107+ ` ;
108+ bookList . appendChild ( li ) ;
109+ } ) ;
110+ }
111+
112+ // Initial render
113+ renderBooks ( ) ;
0 commit comments