Notes error: Entry not found in index when reading view entries - xpages

My xPage SSJS fails in line:
viewEntry = view.getNext(viewEntry);
with error
Notes error: Entry not found in index
I do have this options set to false but it doesn't help:
view.setAutoUpdate(false);
So I suspect that it fails because user has not access to NEXT document because of reader access set. So such document cannot be seen in the view but in TOTALS. How to fix it?
The side problem is that if crashes Domino server then
Here is my code:
var view:NotesView = database.getView("xxxxxxx");
view.setAutoUpdate(false);
var viewNav:NotesViewNavigator = view.createViewNav();
var viewEntry:NotesViewEntry = viewNav.getFirst();
while (viewEntry != null) {
if (viewEntry.isCategory()){
// I work with category entry data
} else if(viewEntry.isTotal()){
// I collect totals
} else {
// I work with view entry
}
var tmpEntry:NotesViewEntry = viewNav.getNext(viewEntry);
viewEntry.recycle();
viewEntry = tmpEntry;
}
It fails in line: viewNav.getNext(viewEntry)
Script interpreter error, line=1001, col=37: [TypeError] Exception occurred calling method NotesViewNavigator.getNext(lotus.domino.local.ViewEntry)
Notes error: Entry not found in index ((xxxxxxx))
tmpEntry:NotesViewEntry = viewNav.getNext(viewEntry);
So how do I really go to next entry if current or next one is invalid?

It may also be worth verifying which entry is not found in index. It could be the first, depending on the context of your code. For example, it might have been updated to take it out of the view. Check for null first. Reader access may also be an issue, if you're working from a ViewNavigator, there are different reasons for access. Use a try/catch to also verify your hypothesis - sessionAsSigner (or ODA's native session) will have access to the next document, which will allow logging to confirm. Once you can confirm the cause, you can code around it.
ViewEntry.isValid() verifies if a soft deletion or user does not have access, as stated in documentation for ViewEntry and Document, which both have the same method.

Use the view navigator. If the user can not access the entry then a simple check with viewentry.getUniversalId() will return null, and so you could even skip it inside the view entries iteration.
Try this code instead:
view.setAutoUpdate(false);
ViewNavigator nav = view.createViewNav();
ViewEntry entry = nav.getCurrent();
ViewEntry nextEntry = null;
while (entry != null) {
if (entry.isCategory()) {
nextEntry = nav.getNextSibling();
} else {
nextEntry = nav.getNext();
}
if (!entry.isTotal()) {
// do something with the entry
} else {
// skipped entry
}
//don't forget to recycle!
entry.recycle();
entry = nextEntry;
}
view.setAutoUpdate(true);

Combine Ferry's answer (get next entry in advance) with the check of validity of the entry: https://www.ibm.com/support/knowledgecenter/en/SSVRGU_9.0.1/basic/H_ISVALID_PROPERTY_2176.html
That should avoid your problem. Also, use try/catch block to identify what is the last processed document and take a closer look at it (and the next one). It may be corrupted.

Related

How to prevent global event handlers from firing caused by an API call

I have a custom module that uses Kentico API (DocumentHelper) to update certain fields of my document and then publish but I do not want it to trigger the event handlers that are linked to my document page type. I tried adding comments to .Publish("admin_edit") hoping that I can catch it from the WorkflowEventargs parameter but the VersionComment property always return null. Is there a way to accomplish this in Kentico?
update field:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn();
document.Publish("admin_edit");
}
event handler:
public override void Init()
{
WorkflowEvents.Publish.After += Publish_After;
}
private void Publish_After(object sender, WorkflowEventArgs e)
{
if (!string.IsNullOrEmpty(e.VersionComment) &&
e.VersionComment.Contains("admin_edit"))
return;
}
You always get null for Version information, because that is related to the 'Page versioning' events, specially for 'SaveVersion'. You can find more about that on this link. If you expand 'Properties' you will see which properties are populated for the specific event. In your case, you can try something like this, to add your message for last version and then check for that comment on 'Publish_After' event, see code bellow:
var document = DocumentHelper.GetDocument(documentID, tree);
var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(document);
if (workflow != null)
{
document.CheckOut();
document.SetValue("SomeFIeld", "some value");
document.Update(true);
document.CheckIn(versionComment: "admin_edit");
document.Publish();
}
and then, in event handler, take last version and check for comment like this:
if (e.PublishedDocument?.VersionHistory?.Count > 0)
{
var lastVersion = e.PublishedDocument.VersionHistory[0] as VersionHistoryInfo;
if (lastVersion.VersionComment.Equals("admin_edit"))
{
return;
}
}
NOTE: In case that you have a lot of concurrent content editors, there is a chance that your last version is not version from API (someone changed content and saved it right after your API call made change). There is a low chance for that, but still is possible. If this is something that you will use often, you must take it in consideration. This code is tested for Kentico 11.

XPages [TypeError] Exception at view.getNextDocument(curDoc)

Here's my set-up:
In my XPage, I have an embedded view which opens a document in the adjacent Panel.
Clicking the doc link in the view sets the doc's UNID in a viewScope variable, and refreshes the Panel. The UNID variable is used to define the Page's Data Source Document (doc).
Now in a Button, I want to get a handle to the next document in the view, but when the following SSJS code is run an error occurs at the last line:
var v:NotesView = database.getView("ViewByYear");
var curDoc:NotesDocument = doc.getDocument();
// curDoc = v.getFirstDocument(); /* if this line is uncommented the error does not occur */
if (curDoc==null) return(false);
var nextDoc = v.getNextDocument(curDoc);
The error message is: Script interpreter error, line=11, col=32: [TypeError] Exception occurred calling method NotesView.getNextDocument(lotus.domino.local.Document) null
if I uncomment the commented line (setting curDoc as the first document in the view), the error does not occur.
Any idea why this is happening? How is the document generated from the Data Source different? This document comes anyway from this same view which is embedded on the same XPage.
Thanks for your insights
The question has been answered and just wanted to add the recycle bit and I'm sure others can benefit it.
var v:NotesView = database.getView("ViewByYear");
//var curDoc:NotesDocument = doc.getDocument(); // not needed
var curDoc:NotesDocument = v.getFirstDocument(); // starting reference as explained by #John
while (curDoc != null){
var nextDoc = curDoc.getNextDocument(curDoc); // to fetch the next document
curDoc.recycle(); // recycle called to manage memory
curDoc = nextDoc; // replacing/passing data between variables
}
Actually, this works exactly as expected :-)
It has nothing to do with type of variables etc. Basically, if you want to traverse a view you need to set the "starting" point which in your case you do with curDoc = v.getFirstDocument();. Then when you want the next document you use nextDoc = v.getNextDocument(curDoc);, i.e. you tell it to return the document following the curDoc. This is the way the backend objects have been working since LotusScript was introduced back in version 4. And it is the same pattern no matter whether you use LotusScript, SSJS or Java :-)
So a common pattern would be:
var v:NotesView = database.getView("ViewByYear");
var curDoc = v.getFirstDocument();
while(null != curDoc){
// Do some work with curDoc
curDoc = v.getNextDocument(curDoc);
}
Now, you may have heard of memory considerations in relation to traversing a view. Basically, Domino will normally take care of memory handling for you. But especially when iterating over large document collections you should use a slightly different pattern (which you can also use for small document collections):
var v:NotesView = database.getView("ViewByYear");
var curDoc = v.getFirstDocument();
var nextDoc = null;
while(null != curDoc){
nextDoc = v.getNextDocument(curDoc);
// Do some work with curDoc
curDoc = null; // reclaim memory
curDoc = nextDoc;
}
/John
I've modified my answer to utilize NotesViewNavigator:
var v:NotesView = database.getView("ViewByYear");
var curDoc:NotesDocument = doc.getDocument();
// curDoc = v.getFirstDocument(); /* if this line is uncommented the error does not occur */
if (curDoc==null) return(false);
var nav:NotesViewNavigator = v.createViewNavFrom(curDoc);
var entry:NotesViewEntry = nav.getNext();
if (entry == null) { return false; } // check to see if curDoc is last doc in view
var nextDoc:NotesDocument = entry.getDocument();
Is it possible curDoc is the last doc in the view in your code?

How can I update a content item (draft) from a background task in Orchard?

I have a simple IBackgroundTask implementation that performs a query and then either performs an insert or one or more updates depending on whether a specific item exists or not. However, the updates are not persisted, and I don't understand why. New items are created just as expected.
The content item I'm updating has a CommonPart and I've tried authenticating as a valid user. I've also tried flushing the content manager at the end of the Sweep method. What am I missing?
This is my Sweep, slightly edited for brevity:
public void Sweep()
{
// Authenticate as the site's super user
var superUser = _membershipService.GetUser(_orchardServices.WorkContext.CurrentSite.SuperUser);
_authenticationService.SetAuthenticatedUserForRequest(superUser);
// Create a dummy "Person" content item
var item = _contentManager.New("Person");
var person = item.As<PersonPart>();
if (person == null)
{
return;
}
person.ExternalId = Random.Next(1, 10).ToString();
person.FirstName = GenerateFirstName();
person.LastName = GenerateLastName();
// Check if the person already exists
var matchingPersons = _contentManager
.Query<PersonPart, PersonRecord>(VersionOptions.AllVersions)
.Where(record => record.ExternalId == person.ExternalId)
.List().ToArray();
if (!matchingPersons.Any())
{
// Insert new person and quit
_contentManager.Create(item, VersionOptions.Draft);
return;
}
// There are at least one matching person, update it
foreach (var updatedPerson in matchingPersons)
{
updatedPerson.FirstName = person.FirstName;
updatedPerson.LastName = person.LastName;
}
_contentManager.Flush();
}
Try to add _contentManager.Publish(updatedPerson). If you do not want to publish, but just to save, you don't need to do anything more, as changes in Orchard as saved automatically unless the ambient transaction is aborted. The call to Flush is not necessary at all. This is the case both during a regular request and on a background task.

The file has been modifed by error in sharepoint

I am getting the following error when i try to update the item's name in the sharepoint
document library. The item is of type document set and its default values is loaded using javascript. In the Item added event we are updating with the new changed item's name value. But in the item.update() code statement i am getting following error.
The File CZY14389 has been modified by domain\username on current date.
Please provide your commens on resolving this.
You cannot change the name of a sharepoint document like that. You need to "move it".
Item.Update();
Item.File.MoveTo(Item.ParentList.RootFolder.Url + "/" + newFileName, false);
Item.File.Item["FileRef"] = newFileName;
Item.File.Update();
before you update item name and call item.update(), can you try to refresh your item like this:
item = item.ParentList.GetItemById(item.ID);
item.name = "xyz";
item.update();
this can sometimes happen in event handler. the problem is the updation process in the event handler is not as the same of the workflow. In event handler for updating you have to use the followitn steps. Dont use Item.Update() as in workflow.
Follow the steps:
• call and disable event firing before your code with : base.EventFiringEnabled = false;
•update your item by calling item.systemUpdate(false);
•enable event firing with : base.EventFiringEnabled = true;
Disable event firing and call your update code, dont forget to enable event firing.
HandleEventFiring handleEventFiring = new HandleEventFiring();
handleEventFiring.DisableHandleEventFiring();
try
{
item.Update();
//if item.Update doesnt work then use(For me item.update worked only on my local not on prod then i used the below)
//item.SystemUpdate(false)
}
finally
{
handleEventFiring.EnableHandleEventFiring();
}
public class HandleEventFiring : SPItemEventReceiver
{
public void DisableHandleEventFiring()
{
//obsolete
//this.DisableEventFiring();
this.EventFiringEnabled = false;
}
public void EnableHandleEventFiring()
{
//obsotete
//this.EnableEventFiring();
this.EventFiringEnabled = true;
}
}

why my sharepoint workflow always stop when i use this code?

I need to find an user in a list to set the assignedto task property, these informations are in a list. So i use this method :
public static SPUser GetSPUser(SPListItem item, string key){
SPFieldUser field = item.Fields[key] as SPFieldUser;
if (field != null)
{
SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
if (fieldValue != null)
{
return fieldValue.User;
}
}
return null;
}
The problem is that when i use this method or this part of code, my workflow stop without saying anything. Here an exemple of code when i use it :
using (SPSite site = new SPSite(adress_of_my_site))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Acteurs du projet"];
SPView view = cobj_ListeDesActeursDuProjet.DefaultView;
SPListItemCollection itemcollection = list.GetItems(view);
foreach (SPListItem item in itemcollection)
{
SPUser lobj_acteur = Utilities.GetSPUser(item,"acteur");
// Dictionary<string,class>
ActeursDuProjet[item["Rôle"].ToString()] =
new ActeursDuProjet()
{
Login = lobj_acteur.LoginName,
Email = lobj_acteur.Email
};
}
}
}
If i comment the content of my foreach my workflow continue as well...
If anybody have an idea it will be cool.
Regards,
Loïc
edit: problem in the code
Here are some debugging tips that might help:
ULS logs
Any exceptions should be reported here in some detail.
Enable debugging for all .NET code
This will cause the debugger to break whenever an exception occurs in SharePoint as well as your code. The downside is that the debugger will break on 'normal' exceptions that cause no side effects. So don't be misled!
To enable: Go to Debug, Exceptions and tick Common Language Runtime Exceptions. Also go to Tools, Options, Debugging and untick Enable Just My Code. Then attach to w3wp.exe.
Commenting code
You could also comment out all of your code. If the workflow step fails, you know there is a problem elsewhere. If the workflow step passes, then start uncommenting code until it fails - then you know where to look.
I tried commenting this above but it didn't format nicely so here it is.
It probably is fine, but this looks fishy to me:
// Dictionary<string,class>
ActeursDuProjet[item["Rôle"].ToString()] =
new ActeursDuProjet()
{
Login = lobj_acteur.LoginName,
Email = lobj_acteur.Email
};
I would think it would be something like:
// dictionary declared somewhere earlier
Dictionary<string,ActeursDuProjet> roles = new Dictionary<string,ActeursDuProjet>();
// inside the foreach
string role = item["Rôle"].ToString();
if (!roles.ContainsKey(role))
{
ActeursDuProjet foo = new ActeursDuProjet();
foo.Login = lobj_acteur.LoginName;
foo.Email = lobj_acteur.Email;
roles.Add(role, foo);
}

Resources