List list = web.Lists.GetByTitle("listtitle");
ListItem item = list.GetItemById(1);
var attachment = new AttachmentCreationInformation();
attachment.FileName = "fileName";
attachment.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes("streamFile"));
Attachment att = item.AttachmentFiles.Add(attachment);
context.Load(att);
context.ExecuteQuery();
Wednesday, January 16, 2019
How to attach files to list item using Client Object Model?
Tuesday, January 8, 2019
The online IDE for web applications. Powered by Visual Studio Code
Show Specific Folder items of Document library in WebPart using Script edit WebPart in SharePoint online
<script type="text/javascript">
// Call the function once page is loaded
SP.SOD.executeFunc('sp.js','SP.ClientContext',runWhenLoaded);
//Shared Global Variables
var clientContext;
var oWeb;
var oList;
var currentUserIndex;
var userCount;
function runWhenLoaded() {
// Create context, get the rootweb, then get the list
clientContext = SP.ClientContext.get_current();
oWeb = clientContext.get_web();
oList = oWeb.get_lists().getByTitle("Documents");
clientContext.load(oList);
var webPartView = oList.getView('{29E784E3-089B-4CF5-A415-018ED441A357}');
clientContext.load(webPartView);
// Execute the queued context commands (passes back to the global vars)
clientContext.executeQueryAsync(function() {
if (webPartView) {
alert('webpartview valid');
webPartView.set_scope(2); // recursiveall
webPartView.set_viewQuery('<Where><Contains><FieldRef Name="FileDirRef"/><Value Type="Text">Strategy</Value></Contains></Where>');
webPartView.update();
clientContext.load(webPartView);
clientContext.executeQueryAsync(Function.createDelegate(this, updateSuccess), Function.createDelegate(this, updateFailed));
function updateSuccess(sender, args) { alert('changed the view'); }
function updateFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }
}
else {
alert('Unable to get view');
}
},function() { alert("Error"); } );
}
</script>
Thursday, January 3, 2019
SharePoint Framework development environment
Install Yeoman and gulp
npm install -g yo gulp
npm install -g @microsoft/generator-sharepoint
If you need to switch between the different projects created by using different versions of the SharePoint Framework Yeoman generator, you can install the generator locally as a development dependency in the project folder by executing the following command:
npm install @microsoft/generator-sharepoint --save-dev
Create a new web part project
To create a new web part project
- Create a new project directory in your favorite location.
md helloworld-webpart
- Go to the project directory.
- Create a new HelloWorld web part by running the Yeoman SharePoint Generator.
yo @microsoft/sharepoint
- When prompted:
- Accept the default helloworld-webpart as your solution name, and then select Enter.
- Select SharePoint Online only (latest), and select Enter.
- Select Use the current folder for where to place the files.
- Select N to allow solution to be deployed to all sites immediately.
- Select N on the question if solution contains unique permissions.
- Select WebPart as the client-side component type to be created.
- The next set of prompts ask for specific information about your web part:
- Accept the default HelloWorld as your web part name, and then select Enter.
- Accept the default HelloWorld description as your web part description, and then select Enter.
- Accept the default No javascript web framework as the framework you would like to use, and then select Enter.
At this point, Yeoman installs the required dependencies and scaffolds the solution files along with the HelloWorldweb part. This might take a few minutes.
When the scaffold is complete, you should see the following message indicating a successful scaffold.
Preview the web part
Developer certificate has to be installed ONLY once in your development environment, so you can skip this step, if you have already executed that in your environment.
gulp trust-dev-cert
Now that we have installed the developer certificate, enter the following command in the console to build and preview your web part:
gulp serve
SharePoint Workbench
SharePoint Workbench is a developer design surface that enables you to quickly preview and test web parts without deploying them in SharePoint.
https://your-sharepoint-site/_layouts/workbench.aspx
https://your-sharepoint-site/_layouts/workbench.aspx
Why the SharePoint Framework?
Reason:-
SharePoint feature XML, DLLs not allowed in cloud.
Two alternative models:
1. client-side JavaScript injection
SharePoint Online is the Script Editor.
big problem is that the Script Editor web part is not marked as "Safe For Scripting". Most self-service site collections (my-sites, team sites, group sites) have a feature known as "NoScript" enabled.
2. SharePoint Add-ins.
This implementation creates an iFrame.
So SharePoint Framework is the next evolution in SharePoint development.
SharePoint feature XML, DLLs not allowed in cloud.
Two alternative models:
1. client-side JavaScript injection
SharePoint Online is the Script Editor.
big problem is that the Script Editor web part is not marked as "Safe For Scripting". Most self-service site collections (my-sites, team sites, group sites) have a feature known as "NoScript" enabled.
2. SharePoint Add-ins.
This implementation creates an iFrame.
So SharePoint Framework is the next evolution in SharePoint development.
Key features of the SharePoint Framework
- It runs in the context of the current user and connection in the browser. There are no iFrames for the customization (JavaScript is embedded directly to the page).
- The controls are rendered in the normal page DOM.
- The controls are responsive and accessible by nature.
- It enables the developer to access the lifecycle in addition to render, load, serialize and deserialize, configuration changes, and more.
- It is framework-agnostic. You can use any JavaScript framework that you like: React, Handlebars, Knockout, Angular, and more.
- The toolchain is based on common open source client development tools such as npm, TypeScript, Yeoman, webpack, and gulp.
- Performance is reliable.
- End users can use SPFx client-side solutions that are approved by the tenant administrators (or their delegates) on all sites, including self-service team, group, or personal sites.
- SPFx web parts can be added to both classic and modern pages.
Azure AD Connect sync: Directory extensions
These attributes are now available through the Azure AD Graph API
Attribute-Mappings in Azure Active Directory
In the Azure AD portal, you can access this feature by clicking a Mappings configuration under Provisioning in the Manage section of an Enterprise application.
Clicking a Mappings configuration, opens the related Attribute-Mapping screen. There are attribute-mappings that are required by a SaaS application to function correctly. For required attributes, the Delete feature is unavailable.
Clicking a Mappings configuration, opens the related Attribute-Mapping screen. There are attribute-mappings that are required by a SaaS application to function correctly. For required attributes, the Delete feature is unavailable.
n the example above, you can see that the Username attribute of a managed object in Salesforce is populated with the userPrincipalName value of the linked Azure Active Directory Object.
You can customize existing Attribute-Mappings by clicking a mapping. This opens the Edit Attribute screen.
Avoid getting throttled or blocked in SharePoint Online
Does this sound familiar? You're running a CSOM process - for
example, to migrate files in SharePoint Online - but you keep getting
throttled. Or even worse, you get completely blocked. What's going on and what
can you do to make it stop?
What is throttling?
SharePoint Online uses throttling to maintain
optimal performance and reliability of the SharePoint Online service.
Throttling limits the number of user actions or concurrent calls (by script or
code) to prevent overuse of resources.
That said, it is extremely rare for a user to get throttled in
SharePoint Online. The service is robust, and it is designed to handle very
high volume. If you do get throttled, 99% of the time it is because of custom
code. That doesn't mean that there aren't other ways to get throttled, just
that they are less common. For example you spin up 10 machines and have a sync
client going on all 10. On each sync 1TB of content. This would likely get you
throttled.
What happens when you get throttled in SharePoint Online?
When a user exceeds usage limits, SharePoint Online throttles any
further requests from that user account for a short period. All user actions
are throttled while the throttle is in effect.
·
For requests that a user
performs directly in the browser, SharePoint Online redirects you to the
throttling information page, and the requests fail.
·
For all other requests,
including CSOM or REST calls, SharePoint Online returns HTTP status code 429
("Too many requests"), and the requests fail.
If the offending process continues to exceed usage limits,
SharePoint Online might completely block the process; in this case, you may see
HTTP status code 503 ("Service unavailable"), and we'll notify you of
the block in the Office 365 Message Center. The error message is shown below:
503 Server unavailable message.
Common throttling scenarios in SharePoint Online
The most common causes of per-user throttling in SharePoint Online
are client-side object model (CSOM) or Representational State Transfer (REST)
code that performs too many actions too frequently.
·
Sporadic
traffic
Not a lot of traffic at any one time, but enough
over time that you run in and out of throttling in an episodic way.
o For example, after migrating files to SharePoint
Online, you run a custom CSOM or REST script to update metadata on the files.
The CSOM/REST script is updating a large number of files at a very high
frequency, which triggers throttling. Similarly, an autocomplete UI widget
using REST services, making too many calls to lists during each end user
operation, may also cause throttling, depending on what other operations are
consuming resources at the same time.
·
Overwhelming
traffic
A single process dramatically exceeds throttling
limits, continually, over a long time period.
o You used web services to build a tool to
synchronize user profile properties. The tool updates user profile properties
based on information from your line-of-business (LOB) human resources (HR)
system. The tool makes calls at too high a frequency.
o You're running a load-testing script on
SharePoint Online and you get throttled. Load testing is not allowed on
SharePoint Online.
o You customized your team site on SharePoint
Online, for example, by adding a status indicator on the Home page. This status
indicator updates frequently, which causes the page to make too many calls to
the SharePoint Online service - this triggered throttling.
Why can't you just tell me the exact throttling limits?
Setting and publishing exact throttling limits sounds very
straightforward, but in fact, it's not the best way to go. We continually
monitor resource usage on SharePoint Online. Depending on usage, we fine-tune
thresholds so users can consume the maximum number of resources without
degrading the reliability and performance of SharePoint Online. That's why it's
so important for your CSOM or REST code to include incremental back off to
handle throttling; this lets your code run as fast as possible on any given
day, and it lets your code back off "just enough" if it hits
throttling limits. The code samples later in this article show you how to use
incremental back off.
Best practices to handle throttling
·
Reduce the frequency of
calls
·
Decorate your traffic so
we know who you are (see section on traffic decoration best practice more on
that below)
If you do run into throttling, we recommend incremental back off
to reduce the number and frequency of calls until no more throttling occurs.
Incremental back off uses progressively longer waits between
retries before trying again to run the code that was throttled. You can use the
GitHub code samples, later in this article, written as extension methods, to
add incremental back off to your code.
Backing off is the fastest way to handle being throttled because
SharePoint Online continues to log resource usage while a user is being
throttled. In other words, aggressive retries work against you because even
though the calls fail, they still accrue against your usage limits. The faster
you back off, the faster you'll stop exceeding usage limits.
For information about ways to monitor your SharePoint Online
activity, see Diagnosing performance issues with SharePoint
Online.
How to decorate your http traffic to avoid throttling?
To ensure and maintain high-availability, some traffic may be
throttled. Throttling happens when system health is at stake and one of the
criteria used for throttling is traffic decoration, which impacts directly on
the prioritization of the traffic. Well decorated traffic will be prioritized
over traffic which is not properly decorated.
What is definition of undecorated traffic?
·
Traffic is undecorated
if there is no AppID/AppTitle and User Agent string in CSOM or REST API call to
SharePoint Online. The User Agent string should be in a specific format as
described below.
What are the recommendation?
·
If you have created an
application, the recommendation is to register and use AppID and AppTitle –
This will ensure the best overall experience and best path for any future issue
resolution. Include also the User Agent string information as defined in
following step.
·
Make sure to include
User Agent string in your API call to SharePoint with following naming convention
Type
|
User Agent
|
Description
|
ISV Application
|
ISV|CompanyName|AppName/Version
|
Identify as ISV and include
Company
Name, App Name separated by a
pipe character and then adding
Version number separated with a
slash character
|
Enterprise application
|
NONISV|CompanyName|AppName/Version
|
Identify as NONISV and include
Company Name, App Name
separated by a pipe character
and then adding Version number
separated with a slash character
|
·
If you are building your
own JavaScript libraries, which are used to call SharePoint Online APIs, make
sure that you include the User Agent information to your http request and
potentially register your web application also as an Application, where
suitable.
Note
Format of the user agent string is expected to
follow RFC2616, so please follow up on the above guidance on the right
separators. It is also fine to append existing user agent string with the
requested information.
Note
If you are developing front end components
executing in the browser, most of modern browsers don't allow overwritting the
user agent string and you don't need to implement this.
Example of decorating traffic with User agent when using Client
Side Object Model (CSOM)
C#Copy
// Get access to source site
using (var ctx = new
ClientContext("https://contoso.sharepoint.com/sites/team"))
{
//Provide account and pwd for connecting to
SharePoint Online
var passWord = new
SecureString();
foreach (char c in
pwd.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials =
new SharePointOnlineCredentials("contoso@contoso.onmicrosoft.com", passWord);
// Add our User Agent information
ctx.ExecutingWebRequest += delegate (object sender, WebRequestEventArgs e)
{
e.WebRequestExecutor.WebRequest.UserAgent
= "NONISV|Contoso|GovernanceCheck/1.0";
};
// Normal CSOM Call with custom User-Agent
information
Web site =
ctx.Web;
ctx.Load(site);
ctx.ExecuteQuery();
}
Example of decorating traffic with User agent when using REST APIs
Following sample is in c# format, but the similar User Agent
information is recommended to be used even for the JavaScript libraries used in
the SharePoint Online pages.
C#Copy
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString()
+ "/_api/web/lists");
endpointRequest.Method = "GET";
endpointRequest.UserAgent = "NONISV|Contoso|GovernanceCheck/1.0";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
HttpWebResponse endpointResponse =
(HttpWebResponse)endpointRequest.GetResponse();
GitHub CSOM code samples: SharePoint Online Throttling
CoreThrottling in the Office 365 Developer Patterns and Practices repository is a code sample that demonstrates the incremental
back off technique. The technique requires minimal changes to your code.
Before you run this code sample:
·
Open Program.cs and
enter the following information in the Main method:
o Your Office 365 Developer account credentials.
o The URL of your Office 365 Developer Site.
o The name of a test document library on your
Office 365 Developer Site.
·
If you receive an error
stating that the App.Config file is invalid, go to Solution
Explorer, right click App.config, and choose Exclude
From Project.
Core.Throttling runs as a console application using a user-only
authorization policy, which means this code sample uses the permissions of the
current user. In the Main method in Program.cs, a while loop
repeatedly creates new folders in the test document library. A call is then
made to ctx.ExecuteQueryWithExponentialRetry, which uses CSOM to
perform the ExecuteQuery method. ExecuteQueryWithExponentialRetry is
an extension method on the ClientContext object, and is defined
in ClientContextExtension.cs.
If SharePoint Online throttles the ExecuteQuery statement, ExecuteQueryWithIncrementalRetry starts
the incremental back off technique by:
·
Catching a WebException and
checking the HttpWebResponse.StatusCode. If SharePoint Online
throttled the ExecuteQuery statement, the HttpWebResponse.StatusCode is
429.
·
The current thread is
suspended for the period specified in backoffInterval.
·
When the current thread
resumes, the backoffInterval is doubled and the number of
retries performed ( retryAttempts) is incremented. By
doubling backoffInterval your code suspends activity for a
longer period of time before retrying the code that was throttled by SharePoint
Online.
·
The process is repeated
until either the ExecuteQuery statement is successful, or the
number of allowed retries ( retryCount ) is exceeded.
CSOM Code sample: Incremental back off and retry (calls
ExecuteQueryWithIncrementalRetry method, later in this article)
Copy
using (var ctx = new ClientContext(serverUrl))
{
//Provide
account and pwd for connecting to the source
var
passWord = new SecureString();
foreach
(char c in password.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials(login, passWord);
try
{
int
number = 0;
//
This loop will be executed 1000 times, which will cause throttling to occur
while
(number < 1000)
{
//
Try to create new folder based on Ticks to the given list as an example process
var folder = ctx.Site.RootWeb.GetFolderByServerRelativeUrl(listUrlName);
ctx.Load(folder);
folder = folder.Folders.Add(DateTime.Now.Ticks.ToString());
//
Extension method for executing query with throttling checks
ctx.ExecuteQueryWithIncrementalRetry(5, 30000); //5 retries, with a base
delay of 30 secs.
//
Status indication for execution.
Console.WriteLine("CSOM request successful.");
//
For loop handling.
number = number + 1;
}
}
catch
(MaximumRetryAttemptedException mex)
{
//
Exception handling for the Maximum Retry Attempted
Console.WriteLine(mex.Message);
}
}
CSOM Code sample: ExecuteQueryWithIncrementalRetry method
Copy
public static void ExecuteQueryWithIncrementalRetry(this
ClientContext context, int retryCount, int delay)
{
int
retryAttempts = 0;
int
backoffInterval = delay;
if (retryCount
<= 0)
throw
new ArgumentException("Provide a retry count greater than zero.");
if (delay
<= 0)
throw
new ArgumentException("Provide a delay greater than zero.");
while
(retryAttempts < retryCount)
{
try
{
context.ExecuteQuery();
return;
}
catch
(WebException wex)
{
var response = wex.Response as HttpWebResponse;
if (response != null && response.StatusCode ==
(HttpStatusCode)429)
{
Console.WriteLine(string.Format("CSOM request exceeded usage
limits. Sleeping for {0} seconds before retrying.", backoffInterval));
//Add delay.
System.Threading.Thread.Sleep(backoffInterval);
//Add to retry count and increase delay.
retryAttempts++;
backoffInterval =
backoffInterval * 2;
}
else
{
throw;
}
}
}
throw new
MaximumRetryAttemptedException(string.Format("Maximum retry attempts {0},
have been attempted.", retryCount));
}
What should you do if you get blocked in SharePoint Online?
Blocking is the most extreme form of throttling. We rarely ever
block a tenant, unless we detect long-term, extremely excessive traffic that
may threaten the overall health of the SharePoint Online service. We apply
blocks to prevent excessive traffic from degrading the performance and
reliability of SharePoint Online. A block - which is usually placed at the
tenancy level - prevents the offending process from running until you fix the
problem. If we block your subscription, you must take action to modify the
offending processes before the block can be removed.
If we block your subscription, you'll see HTTP status code 503,
and we'll notify you of the block in the Office 365 Message Center. The message
describes what caused the block, provides guidance on how to resolve the
offending issue, and tells you who to contact to get the block removed.
Subscribe to:
Posts (Atom)