Write to eventlog from within Sharepoint webpart - sharepoint

I'm trying to create a custom webpart. To implement error handling I would like to write to the eventlog. To do so, I'm trying to use the following code;
protected void btnExceptionTester_Click(object sender, EventArgs e)
{
try
{
throw new Exception("this is a test");
}
catch (Exception ex)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
EventLog.WriteEntry("TestWebpart", ex.ToString(), EventLogEntryType.Error);
});
}
}
When I try to execute this code, I receive an sharepoint error page (unhandled exception). When I look in the eventlog, I see the following message;
"Requested registry access is not allowed".
I'm runnning (for testing only) under the full trust level. Can someone point out to me what kind of privileges I would need to write to the eventlog? Or is there another approach?
Help greatly appriciated!

I haven't tried writing to the Windows event log but as an alternative you can write to the SharePoint logs in the 12 hive using the following:
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("your message here!");
Hope this helps
Iain

EventLog Entries in SharePoint:
For WebParts Log the errors in the ULS logs which is present in 12 hive.
For SharePoint Application pages, wrtie the errors in the Event Logs.
All other list and Library related errors will be noted down in the ULS logs only.
Thanks

Update
Writing to the event log requires elevated privileges in the web part:
SPSecurity.RunWithElevatedPrivileges(delegate { EventLog.WriteEntry(...
The permissions on:
HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\YourWebPartLog
I think are the cause of your grief. What are the permissions on that vs. the permissions of your application pool?

Related

UserCustomAction does not fire for non-tenant admin in SharePoint online

I have written a fairly straight-forward JavaScript for SharePoint Online that I am deploying as a user custom action into the root site collection using CSOM.
The following code is the block I am using for provisioning the script to the site collection:
public static void ProvisionScriptLinkCustomAction(ClientContext ctx, string name, string url)
{
Site site = ctx.Site;
ctx.Load(site.UserCustomActions);
ctx.ExecuteQuery();
foreach (UserCustomAction action in site.UserCustomActions)
{
if (action.Name == name)
{
action.DeleteObject();
break;
}
}
ctx.ExecuteQuery();
UserCustomAction customAction = site.UserCustomActions.Add();
customAction.Location = "ScriptLink";
customAction.Name = name;
customAction.ScriptSrc = url + "?" + Guid.NewGuid().ToString();
customAction.Update();
ctx.ExecuteQuery();
}
I am logged in as the tenant administrator when deploying and initially testing the custom action. Everything works as expected until I log in as a user that is not the tenant admin. When logged in as any other user that is only a site collection administrator or lower, the custom action does not fire and all of my efforts are for naught.
This feels like a configuration or permissions issue to me but I am at a loss to begin to know where to look to rectify this issue.
Any helpful thoughts would be greatly appreciated!
Turns out I had console.log calls in my JavaScript. Some versions of IE apparently do not know how to log to the console when in browser mode without debug tools which presumably throws an uncaught and unlogged exception. Removing the console.log statements made the issue go away.

Azure App Service - Some WebAPI methods throw OWIN Exception

I have an Azure Mobile App that has some methods that generate 500 errors but does not record any exceptions in Application Insights and no exceptions are thrown inside my code. I have been able to determine that normal TableController methods work fine, but custom methods do not. Also, I can remote debug the code and watch it finish executing without any exceptions being thrown. It should also be noted that I did not have this problem when this project was a Mobile Service. Here is an example method that fails:
private readonly MobileServiceContext context; //Autofac injection
private readonly IUserHelper userHelper; //Autofac injection
[HttpGet, Route("api/Site/{id}/Users")]
public async Task<HttpResponseMessage> Users(string id)
{
var userId = await userHelper.GetUserIdAsync(User, Request);
var query = context.UserSiteMaps.Include(x => x.User).Where(map => map.SiteId == id);
var auth = query.FirstOrDefault(x => x.UserId == userId && x.IsAdmin);
if (auth != null)
{
return Request.CreateResponse(HttpStatusCode.OK, query.Select(map => map.User));
}
return Request.CreateUnauthorizedResponse();
}
The deepest error log that I have been able to obtain is the detailed error page from IIS:
Module __DynamicModule_Microsoft.Owin.Host.SystemWeb.OwinHttpModule, Microsoft.Owin.Host.SystemWeb, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35_9de2321b-e781-4017-8ff3-4acd1e48b129
Notification PreExecuteRequestHandler
Handler ExtensionlessUrlHandler-Integrated-4.0
Error Code
0x00000000
I haven't been able to generate a more detailed error message and I have no idea what Owin is upset about here since other method return requests just fine. Am I doing something I shouldn't?
Any help would be greatly appreciated!
Update : Here is the full error message that I have been able to get.
I have also been able to narrow the cause down a bit. If I replace the query.Select(map => map.User) object in the response with a simple string, it returns that string without complaint. However, if I stringify the response myself and pass that in, I get 500s again. Could it be some serializer setting problem?
The best way to track down the issue is to turn on exception stack traces for you app and to turn on logging on your Mobile App backend.
See Server Side Logging in the Mobile Apps wiki and Enable diagnostics logging for web apps in Azure App Service. You can also remote debug your service to see the exact error, see Remote debugging .NET server SDK.

OTA InitConnectionEx, Login -> Exception: ServerThread DB_TIME_COUNT=

I'm having a c# application which uses OTA-Com-Library to login to ALM. Every time I try to login I get the following exception:
{"<ServerThread DB_TIME_COUNT=\"0\" DB_TIME_MAX=\"0\" SERVER_MACHINE_NAME=\"GRZWINSV1371\" DB_TIME_MIN=\"0\" PROJECT=\"N/A\" THREAD_ID=\"62ED8023-E88C-4CCE-85C0-DFE7D0B108B7\" FS_TIME_MIN=\"0\" SERVER_START_TIME_MS=\"1462181946133\" SERVER_TOTAL_TIME=\"2\" FS_TIME_AVG=\"0\" THREAD_TYPE=\"Login\" SERVER_CPU_TIME=\"0\" FS_TIME_MAX=\"0\" USER_NAME=\"N/A\" FS_TIME_COUNT=\"0\" PROJECT_SESSION_ID=\"-1\" LOGIN_SESSION_ID=\"-1\" THREAD_CATEGORY=\"FREC_REQUEST_CALL\" SERVER_START_TIME=\"2016-05-02 11:39:06\" DB_TIME_AVG=\"0\" />"}
This is the c# code:
try
{
this.server = server;
this.username = user;
this.pwd = password;
tdConn.InitConnectionEx(server);
tdConn.Login(user, password);
}
catch (Exception exception)
{
ExceptionUtils.ReportHandledException(exception);
}
I registered the COM Components by using "Register HP ALM" of provided on this site:
https://centre.qualitat.solucions.gencat.cat/qcbin/CommonMode_index.html
Cheers,
Stefan
Ensure url spesified correctly.
URL to init OTA connection need to be specified in format: http://<server_host>:<server_port>/qcbin
The message you specified comes from server, but not looks like exception.
Please specify full exception with stack trace. Additional information could be obtained from WebGate log. To turn it on follow instruction:
Go to your server apps: https://centre.qualitat.solucions.gencat.cat/qcbin/Apps/
Click "Client Logs Configuration Tool"
When tool downloads run it
Go to the "WebGate" section
For "Log Level" choose "ALL - Log everything" and specify the path wehere log should write to.
Then reproduce the problem and observe WebGate log at path specified and look for text highlighted with red or just provide the output adter "Login Started:" and before "Login Ended:" in the log file.

How to auto login owa web part in sharepoint 2010

I am working on something like this sync e-mail outlook2010 and sharepoint2010 I am looking for a way to log in outlook web parts in sharepoint 2010 automatically using logged in sharepoint user is it possible in C#.. i want to get away from kerberos authentication and configuring exchange server I've tried this one Get Current User Inbox and other similar ones but it didn't work.
any suggestions?
Are the login credentials the same as for windows? If so, have you configured the browser correctly? See browser setup for auto login
To login try the following, inherit from OWAInboxPart and in OnInit create the mailbox name using the current user, for some a simple question mark seems to work:
public class MyInbox : Microsoft.SharePoint.Portal.WebControls.OWAInboxPart
{
protected override void OnInit(object sender, EventArgs e)
{
this.MailboxName = ?;
base.OnInit (sender, e);
}
}
Look at this discussion of how to get the mailbox for the current user.

Web Part Error: This page has exceeded its data fetch limit for connected Web Parts

I have a display form with two custom list forms and both are connected to each other and they display the results according to the filter. But when ever I sort on any field, it gives the following error:
Web Part Error: This page has exceeded its data fetch limit for connected Web Parts. Try disconnecting one or more Web Parts to correct the problem.
I appreciate any help.
Thanks
SP
I ran into this issue today while building an IFilterProvider web part.
For me, the solution was to explicitly fire NoFilter in the PartCommunicationMain method. It seems at least NoFilter, ClearFilter or SetFilter must be fired each time this method is invoked.
public override void PartCommunicationMain()
{
// Ensure that all of the Web Part's controls are created.
EnsureChildControls();
TriggerNoFilter(this, null);
}
protected virtual void TriggerNoFilter(object sender, EventArgs e)
{
if (NoFilter != null)
NoFilter(sender, e);
}

Resources