Showing posts with label Client object model. Show all posts
Showing posts with label Client object model. Show all posts

Thursday, December 24, 2020

Unauthorized exception while downloading file from SharePoint

In this article, How to download the SharePoint  library files in Document Library


 var file = clientContext.Web.GetFileByServerRelativeUrl(fileRef);

                clientContext.Load(file);

                clientContext.ExecuteQuery();                    

                ClientResult<Stream> streamResult = file.OpenBinaryStream();

                clientContext.ExecuteQuery();

#sharepoint #SharePoint Developer/  #O365 #Administrator/ #NET #jQuery #AngularJS #poweapps #powerautomate #spfx #pnp #workflo

Thursday, October 20, 2016

Client Object Model vs Server Object Model?

Server Object Model?
SharePoint server where the Server Object Model binaries

Example

inside Web Parts / Workflows.

Client Object Model?

client machines where the entire SharePoint binaries are not available.

Example
Windows Forms application in a client machine.

Update list item or change item value using Client Object Model


"Client" in the namespace.  For example, in the server object model you have:
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.SPList

In the client object model you have:
Microsoft.SharePoint.Client.Site
Microsoft.SharePoint.Client.Web
Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");

Web web = context.Web;

List list = web.Lists.GetByTitle("yourlistname");

CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";

ListItemCollection listItems = list.GetItems(query);

context.Load(listItems);

context.ExecuteQuery();

ListItem item = listItems[listItems.Count - 1];
item["Status"] = "Complete";
item.Update();

context.ExecuteQuery();

Add new item or Insert new item using CLient Object Model


"Client" in the namespace.  For example, in the server object model you have:
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.SPList

In the client object model you have:
Microsoft.SharePoint.Client.Site
Microsoft.SharePoint.Client.Web
Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");

Web web = context.Web;

List list = web.Lists.GetByTitle("yourlistname");

ListItemCreationInformation newItem = new ListItemCreationInformation();
ListItem listItem = list.AddItem(newItem);
listItem["Title"] = "Peakfinder";
listItem.Update();

context.ExecuteQuery();

Get By Search using Client Object Model


"Client" in the namespace.  For example, in the server object model you have:
Microsoft.SharePoint.SPSite
Microsoft.SharePoint.SPWeb
Microsoft.SharePoint.SPList

In the client object model you have:
Microsoft.SharePoint.Client.Site
Microsoft.SharePoint.Client.Web
Microsoft.SharePoint.Client.List


NameSpace:


Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll


Example Code:

ClientContext context = new ClientContext("http://peakfinders.com");
List list = context.Web.Lists.GetByTitle("yourlistname");
CamlQuery query = new CamlQuery();
query.ViewXml = @"<View>
    <Query>
        <Where>
        <Eq>
            <FieldRef Name='Status'/>
            <Value Type='Text'>Pending</Value>
        </Eq>
        </Where>
    </Query>
    </View>";

ListItemCollection listItems = list.GetItems(query);
context.Load(listItems, items => items.Include(
                                                item => item["Name"],
                                                item => item["Designation"],
                                                item => item["Status"]
                                                ));
context.ExecuteQuery();